调用加法函数计算输入参数之和

Calling addition function to calculate sum of input parameters

我有这组代码(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 网络服务调用的加法来计算总和并将其显示为例如{"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));
  }
}  

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

问题原来是 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;
        }

如果有人对此问题的解决方案较少,请分享。