繁体   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