我有自己的Web API应用程序,我想在其中调用服务器上的另一个api。我怎么能这样做?
var result = url(http://54.193.102.251/CBR/api/User?EmpID=1&ClientID=4&Status=true);
// Something like this.
答案 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