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 CookieContainer
s 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".
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.
In the preceding code, AddHttpClient registers GitHubService as a transient service. This registration uses a factory method to: Create an instance of HttpClient .
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.
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.
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 HttpClient
s.
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 inCookieContainer
objects being shared. UnanticipatedCookieContainer
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, }; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With