简体   繁体   English

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

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

I have an ASP.NET Web API which is supposed to call a java addition web service.我有一个 ASP.NET Web API,它应该调用一个 Java 附加 Web 服务。 When i run the java web service and type url http://localhost:8080/addition/9/6 i get {"firstNumber":9,"secondNumber":6,"sum":15} as the output data.当我运行 Java Web 服务并输入 url http://localhost:8080/addition/9/6我得到{"firstNumber":9,"secondNumber":6,"sum":15}作为输出数据。 Right now, i want to use the ASP.NET Web API to call and display that data when i run the ASP.NET Web API application.现在,我想在运行 ASP.NET Web API 应用程序时使用 ASP.NET Web API 来调用和显示该数据。 How do i go about doing that?我该怎么做?

Here are my codes:这是我的代码:

ASP.NET Web API Codes ASP.NET Web API 代码

RestfulClient.cs 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;
        }
    }
}

ApiController.cs接口控制器.cs

private RestfulClient restfulClient = new RestfulClient();

    public ActionResult Index()
    {

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

Java Web Service Codes Java Web 服务代码

AdditionController.java加法控制器.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));
  }
}  

Someone please help me.有人请帮助我。 Thank you so much in advance.非常感谢你。

According to the Java service, the URL you are calling from the client is not formatted correctly based on your base URL and the one used in the GetAsync .根据 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;
    }
}

The controller would also need to be updated.控制器也需要更新。

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

Note the proper use of the HttpClient as suggested in the comments and as well as the use of async/await.请注意如注释中建议的HttpClient的正确使用以及 async/await 的使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM