Как использовать веб-API из приложения Asp.net?

У меня есть свое собственное приложение Web API, в котором я хочу вызвать другой API, который находится на сервере. Как я могу это сделать?

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

1 ответ

Решение

Ты можешь использовать HttpClient, Вот образец async метод, который вызывает ваш 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
Другие вопросы по тегам