如何从Asp.net web api应用程序中使用web api

时间:2017-06-15 17:23:58

标签: c# asp.net-web-api asp.net-mvc-apiexplorer

我有自己的Web API应用程序,我想在其中调用服务器上的另一个api。我怎么能这样做?

var result = url(http://54.193.102.251/CBR/api/User?EmpID=1&ClientID=4&Status=true);
// Something like this.

1 个答案:

答案 0 :(得分:1)

您可以使用HttpClient。以下是调用API的async方法示例:

var client = new HttpClient();
client.BaseAddress = new Uri("http://54.193.102.251/CBR/api");
// here you can add additional information like authorization or accept headers
client.DefaultRequestHeaders
   .Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

// you can chose which http method to use
var response = await client.GetAsync("User?EmpID=1&ClientID=4&Status=true");
if (!response.IsSuccessStatusCode)
    return; // process error response here

var json = await response.Content.ReadAsStringAsync(); // assume your API supports json
// deserialize json here