DEV Community


Posted on

Quote : "You're (probably still) using HttpClient wrong and it is destabilizing your software" from Josef

I have tried to use IHTTPClient in my .net core MVC app

In Startup.cs

services.AddHttpClient("Visites", configureClient: client => {
    client.BaseAddress = new Uri("http://xx-yy.abcd/visiteapi/visites/");
    client.DefaultRequestHeaders.Add(name: "Accept", value: "application/json");

}).ConfigurePrimaryHttpMessageHandler(() =>
    return new HttpClientHandler()
        UseDefaultCredentials = true,

In VisitesController.cs

public async Task<ActionResult> LastVisites()
    var httpClient = _httpClient.CreateClient("Visites");
    var data = await httpClient.GetStringAsync("lastvisites?agIdent=3631&nbVisites=5");
    IList<Visite> lastVisites= DeserializeObject<IList<Visite>>(data);
    return View(lastVisites); ;

Here is a very nice article from Josef explaining why use IHTTPClient

Using HttpClient

Top comments (0)