2017-06-15 138 views
-1

我有我自己的Web API應用程序,我想在其中調用服務器上的另一個api。我怎樣才能做到這一點 ?如何從Asp.net web api應用程序中使用web api

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

的[?我如何使用C#的REST API調用(可能的複製https://stackoverflow.com/questions/9620278/how- DO-I-使通話到A-REST的API,使用-C) – CodeCaster

回答

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 
相關問題