Why is Httpclient Baseddres not working?

Asked

Viewed 30 times

0

Below my code:

using (var handler = new HttpClientHandler())
using (var client = new HttpClient(handler))
{
    client.BaseAddress = new Uri("http://localhost:porta/");

    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    // ESSA LINHA DA O ERRO
    HttpResponseMessage response = await client.GetAsync("api/User/ObterTodosPorId/" + id);
    if (response.IsSuccessStatusCode)
    {
        string data = await response.Content.ReadAsStringAsync();
        if (data != null)
        {
            var varUsuario = JsonConvert.DeserializeObject<UsuarioModel>(data);
            usuario = varUsuario;
        }
    }
};

I’m trying to get a GET on http://localhost:port/api/User/Obttodosporid/1 (id = 1 example)

But when it hits the line I commented there in the code, no error happens, the system continues and the list that would be filled with this data is blank. (I have tested with a local fill and it worked).

  • 1

    The commentary says "THAT LINE OF ERROR", but in the question you say "no error occurs". After all, you have a mistake or you don’t have one?

  • Could you better describe this error that happened ? ?

  • I did some tests this morning, identified that the port of the host site was incorrect, changed and worked, thanks.... In any case, no error appeared, nothing, it only continued the execution of the system...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.