【问题标题】:How to consume web api from Asp.net web api application如何从 Asp.net web api 应用程序使用 web api
【发布时间】:2017-06-15 17:23:57
【问题描述】:
我有自己的 Web API 应用程序,我想在其中调用服务器上的另一个 api。我该怎么做?
var result = url(http://54.193.102.251/CBR/api/User?EmpID=1&ClientID=4&Status=true);
// Something like this.
【问题讨论】:
标签:
c#
asp.net-web-api
asp.net-mvc-apiexplorer
【解决方案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