How to consume web api from Asp.net web api application

988 Views Asked by At

I have my own Web API application in which i want to call the another api which is on server. How can i do that ?

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

There are 1 best solutions below

0
Sergey Berezovskiy On BEST ANSWER

You can use HttpClient. Here is a sample of async method which calls your API:

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