簡體   English   中英

使用 ASP.NET Web API 調用 Java Web Service

[英]Using ASP.NET Web API to call Java Web Service

我有一個 ASP.NET Web API,它應該調用一個 Java 附加 Web 服務。 當我運行 Java Web 服務並輸入 url http://localhost:8080/addition/9/6我得到{"firstNumber":9,"secondNumber":6,"sum":15}作為輸出數據。 現在,我想在運行 ASP.NET Web API 應用程序時使用 ASP.NET Web API 來調用和顯示該數據。 我該怎么做?

這是我的代碼:

ASP.NET Web API 代碼

RestfulClient.cs

public class RestfulClient     
{
    private string BASE_URL = "http://localhost:8080/addition/";
    public Task<string> addition()
    {
        {
            try
            {
                var client = new HttpClient();
                client.BaseAddress = new Uri(BASE_URL);
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = client.GetAsync("addition").Result;
                return response.Content.ReadAsStringAsync();                 
            }
            catch (Exception e)
            {
                HttpContext.Current.Server.Transfer("ErrorPage.html");
            }
            return null;
        }
    }
}

接口控制器.cs

private RestfulClient restfulClient = new RestfulClient();

    public ActionResult Index()
    {

        var Result1 = restfulClient.addition().Result;
        return Content(Result1);
    }

Java Web 服務代碼

加法控制器.java

@RestController
public class AdditionController {

private static final String template = " %s";
private static int getSum;

@RequestMapping("/addition/{param1}/{param2}")
@ResponseBody 

public Addition getSum 
            (@PathVariable("param1") int firstNumber,@PathVariable("param2") int secondNumber) {
return new Addition(
        (String.format(template, firstNumber)), String.format(template, secondNumber));
  }
}  

有人請幫助我。 非常感謝你。

根據 Java 服務,根據您的基本 URL 和GetAsync使用的 URL,您從客戶端調用的 URL 的格式不正確。

public class RestfulClient {
    private static HttpClient client;
    private static string BASE_URL = "http://localhost:8080/";

    static RestfulClient() {
        client = new HttpClient();
        client.BaseAddress = new Uri(BASE_URL);
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
    }

    public async Task<string> addition(int a, int b) {
        try {
            var endpoint = string.Format("addition/{0}/{1}", a, b);
            var response = await client.GetAsync(endpoint);
            return await response.Content.ReadAsStringAsync();                 
        } catch (Exception e) {
            HttpContext.Current.Server.Transfer("ErrorPage.html");
        }
        return null;
    }
}

控制器也需要更新。

public async Task<ActionResult> Index() {
    int a = 9;
    int b = 6;
    var result = await restfulClient.addition(a, b);
    return Content(result);
}

請注意如注釋中建議的HttpClient的正確使用以及 async/await 的使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM