使用ASP.NET Web API调用Java Web服务
我有一个ASP.NETWebAPI,它应该调用java附加Web服务。当我运行java web服务并键入url使用ASP.NET Web API调用Java Web服务,java,c#,asp.net,Java,C#,Asp.net,我有一个ASP.NETWebAPI,它应该调用java附加Web服务。当我运行java web服务并键入urlhttp://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 publ
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;
}
}
}
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));
}
}
有人请帮帮我。非常感谢您。根据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;
}
}
公共类RestfulClient{
私有静态HttpClient;
私有静态字符串BASE_URL=”http://localhost:8080/";
静态RestfulClient(){
client=新的HttpClient();
client.BaseAddress=新Uri(基本URL);
client.DefaultRequestHeaders.Accept.Add(
新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
}
公共异步任务添加(int a、int b){
试一试{
var endpoint=string.Format(“addition/{0}/{1}”,a,b);
var response=await client.GetAsync(端点);
return wait response.Content.ReadAsStringAsync();
}捕获(例外e){
HttpContext.Current.Server.Transfer(“ErrorPage.html”);
}
返回null;
}
}
控制器也需要更新
public async Task<ActionResult> Index() {
int a = 9;
int b = 6;
var result = await restfulClient.addition(a, b);
return Content(result);
}
公共异步任务索引(){
INTA=9;
int b=6;
var result=等待restfulClient.addition(a,b);
返回内容(结果);
}
请注意注释中建议的
HttpClient
的正确使用以及async/await的使用。与您的问题无关,但请通过您的应用程序使您的HttpClient成为一个静态实例(请参阅和此)。顺便说一句,您必须使用await
异步方法,否则无法获得结果。好的,谢谢你much@SusmithaNaidu当前的问题是什么?首先,当我的java web服务没有接受任何参数时,这个restfulClient.cs类工作正常。但是,当我向java web服务添加参数时,这个类不起作用。我不确定该如何编辑它。您能指导我吗?我得到一个错误非静态字段、方法或属性“RestfulClient.BASE\u URL”需要对象引用我解决了它。添加了private static string BASE\u URL
这就是我要找的。非常感谢你!
public async Task<ActionResult> Index() {
int a = 9;
int b = 6;
var result = await restfulClient.addition(a, b);
return Content(result);
}