Considere o código a seguir, onde BaseAddressdefine um caminho URI parcial.
using (var handler = new HttpClientHandler())
using (var client = new HttpClient(handler))
{
client.BaseAddress = new Uri("http://something.com/api");
var response = await client.GetAsync("/resource/7");
}
Eu espero que isso execute um GET solicitação para http://something.com/api/resource/7. Mas isso não acontece.
Após algumas pesquisas, acho esta pergunta e resposta: HttpClient with BaseAddress . A sugestão é colocar /no final doBaseAddress .
using (var handler = new HttpClientHandler())
using (var client = new HttpClient(handler))
{
client.BaseAddress = new Uri("http://something.com/api/");
var response = await client.GetAsync("/resource/7");
}
Ainda não funciona. Aqui está a documentação: HttpClient.BaseAddress O que está acontecendo aqui?