DEV Community

artydev
artydev

Posted on

2 2

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)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay