Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do pooled HttpClient instances keep the CookieContainer?

This answer shows how to add a cookie to an HttpClient request. But one of the comments warns that CookieContainer is cached on the HttpClient. Most of my cookies should not be sent again for a different user.

I am hoping that the HttpClientFactory cleans things like added CookieContainers off the instance of the HttpClient that is in its pool.

If I get my HttpClient by calling httpClientFactory.CreateClient is there a chance that the one I get will have a previous CookieContainer on it?

Update:

The answer to my question is yes! CookieContainer is intended for when you want to send the same cookie with every call.

If you need to send a different cookie with each call, there are several ways to do that. But the easiest is to put it in the header.

Because the HttpClientFactory creates a new HttpClient with each request for a client, you can add to the headers of the client and not worry about it reusing the cookie.

The key to doing this is the setting UseCookies. It HAS to be set to false or the cookies you add to the headers will be ignored. Once you do that, you can add the cookies to your header under the key "Cookies" and enter them as semicolon separated key value pairs (with an equal sign between them). Your cookies come back in the header "Set-Cookies".

like image 473
Vaccano Avatar asked Dec 10 '20 01:12

Vaccano


People also ask

Does HttpClient need to be disposed?

There is no need to dispose of the HttpClient instances from HttpClientFactory. Disposal will not actually do anything in this case because the factory manages the handler and connection lifetimes and not the HttpClient instances.

Is AddHttpClient transient?

In the preceding code, AddHttpClient registers GitHubService as a transient service. This registration uses a factory method to: Create an instance of HttpClient .

Can you reuse HttpClient C#?

When you use the same instance of HttpClient for multiple requests (sequential and concurrent) to the same URL, it'll reuse connections. Requests that get to reuse a connection are 5.5-8.5x faster than requests that have to open a new connection.

What is HttpClientFactory?

IHttpClientFactory is a contract implemented by DefaultHttpClientFactory , an opinionated factory, available since . NET Core 2.1, for creating HttpClient instances to be used in your applications.


1 Answers

It's the HttpMessageHandler instances that get pooled; not the HttpClient instances. By default, the handlers have a lifetime of 2 minutes, which means it's very possible that a CookieContainer will be shared between multiple HttpClients.

There's a section in the official docs, which explains that automatic cookie handling and IHttpClientFactory don't work well together:

The pooled HttpMessageHandler instances results in CookieContainer objects being shared. Unanticipated CookieContainer object sharing often results in incorrect code. For apps that require cookies, consider either:

  • Disabling automatic cookie handling
  • Avoiding IHttpClientFactory

Call ConfigurePrimaryHttpMessageHandler to disable automatic cookie handling:

services.AddHttpClient("configured-disable-automatic-cookies")
    .ConfigurePrimaryHttpMessageHandler(() =>
    {
        return new SocketsHttpHandler()
        {
            UseCookies = false,
        };
    });
like image 55
Kirk Larkin Avatar answered Sep 23 '22 18:09

Kirk Larkin