我有这组代码(ASP.NET Web应用程序),它调用java Web服务的附加功能。
RestfulClient.cs
public class RestfulClient
{
private static HttpClient client;
internal Task addition(object firstNumber, object secondNumber)
{
throw new NotImplementedException();
}
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 firstNumber, int secondNumber)
{
try
{
var endpoint = string.Format("addition/{0}/{1}", firstNumber, secondNumber);
var response = await client.GetAsync(endpoint);
return await response.Content.ReadAsStringAsync();
}
catch (Exception e)
{
HttpContext.Current.Server.Transfer("ErrorPage.html");
}
return null;
}
}
我应该在控制器中添加哪些代码来调用添加功能?我希望用户在URL中输入两个数字(当我运行我的ASP.NET Web API时),我希望ASP.NET Web API能够使用我从java Web服务调用的添加方法来计算求和并将其显示为例如{"firstNumber":9,"secondNumber":6,"sum":15}
。
AdditionController.cs
public class AdditionController : ApiController
{
private RestfulClient restfulClient = new RestfulClient();
public IHttpActionResult Get(int firstNumber, int secondNumber)
{
//What do i have to include here?
//Tried
var result = restfulClient.addition(firstNumber, secondNumber);
return Json(result);
}
}
我尝试了一些代码但是我没有得到任何输出。页面只是在不给我任何输出的情况下继续加载。
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "AdditionApi",
routeTemplate: "api/addition/{firstNumber}/{secondNumber}",
defaults: new { action = "Get", controller = "Addition" }
);
}
}
Addition.java
public class Addition {
private int firstNumber, secondNumber, sum;
public Addition(String firstNumber, String secondNumber) {
this.firstNumber = Integer.parseInt(firstNumber.trim());
this.secondNumber = Integer.parseInt(secondNumber.trim());
updateSum();
}
public int getFirstNumber() {
return firstNumber;
}
public void setFirstNumber(int firstNumber) {
this.firstNumber = firstNumber;
updateSum();
}
public int getSecondNumber() {
return secondNumber;
}
public void setSecondNumber(int secondNumber) {
this.secondNumber = secondNumber;
updateSum();
}
public int getSum() {
return sum;
}
public void updateSum() {
this.sum = this.firstNumber + this.secondNumber;
}
@Override
public String toString() {
return String.format("Addition [firstNumber=%d, secondNumber=%d, sum=%d%n", //
firstNumber, secondNumber, sum);
}
}
AdditionController.java
@RestController
public class AdditionController {
private static final String template = " %s";
private static int getSum;
@RequestMapping("/addition/{param1}/{param2}")
@ResponseBody
public Addition addition
(@PathVariable("param1") int firstNumber,@PathVariable("param2") int secondNumber) {
return new Addition(
(String.format(template, firstNumber)), String.format(template, secondNumber));
}
}
有人请帮助我。非常感谢你。
答案 0 :(得分:1)
问题结果是,在RestfulClient.adition中,程序被卡在了一行:var response = await client.GetAsync(endpoint);永远不会收到回复。现在修复可能是:
public string addition(int firstNumber, int secondNumber)
{
try
{
var endpoint = string.Format("addition/{0}/{1}", firstNumber, secondNumber);
HttpResponseMessage response = client.GetAsync(endpoint).Result;
//This will check whether or not the response was executed successfully(status code 200ish or not
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsStringAsync().Result;
}
else
{
throw new Exception($"The server responded with a bad request of type: {response.StatusCode}");
}
}
catch (Exception e)
{
HttpContext.Current.Server.Transfer("ErrorPage.html");
}
return null;
}
如果有人为解决这个问题而解决问题,请分享。