如何从 Asp.net web api 应用程序使用 web api
How to consume web api from Asp.net web api application
我有自己的 Web API 应用程序,我想在其中调用服务器上的另一个 api。我该怎么做?
var result = url(http://54.193.102.251/CBR/api/User?EmpID=1&ClientID=4&Status=true);
// Something like this.
您可以使用 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
我有自己的 Web API 应用程序,我想在其中调用服务器上的另一个 api。我该怎么做?
var result = url(http://54.193.102.251/CBR/api/User?EmpID=1&ClientID=4&Status=true);
// Something like this.
您可以使用 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