【问题标题】:Using ASP.NET Web API to call Java Web Service使用 ASP.NET Web API 调用 Java Web Service
【发布时间】:2017-10-27 01:54:19
【问题描述】:

我有一个 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;
        }
    }
}

ApiController.cs

private RestfulClient restfulClient = new RestfulClient();

    public ActionResult Index()
    {

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

Java Web 服务代码

AdditionController.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));
  }
}  

有人请帮助我。非常感谢您。

【问题讨论】:

  • 与您的问题无关,但请通过您的应用程序将您的 HttpClient 设为静态实例(请参阅MSDN 和此blog post)。
  • 顺便说一句,你必须await async 方法,否则你不会得到他们的结果。见here
  • 好的,非常感谢
  • @SusmithaNaidu 目前的问题是什么?
  • 起初,当我的 java web 服务没有接受任何参数时,这个 restfulClient.cs 类起作用了。但是,当我将参数添加到我的 java web 服务时,这个类不起作用。我不确定我应该如何编辑它。你能指导我完成吗?

标签: java c# asp.net


【解决方案1】:

根据 Java 服务,您从客户端调用的 URL 的格式不正确,基于您的基本 URL 和 GetAsync 中使用的 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);
}

注意 cmets 中建议的 HttpClient 的正确使用以及 async/await 的使用。

【讨论】:

  • 我收到错误An object reference is required for the non-static field, method, or property 'RestfulClient.BASE_URL'
  • 好的,我解决了。已添加private static string BASE_URL
  • 这就是我想要的。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-29
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2018-12-30
  • 2015-04-10
  • 1970-01-01
相关资源
最近更新 更多