Sean Sherman
Sean Sherman

Reputation: 337

HttpClient and Connection: close HttpRequestHeaders

I'm making a static wrapper for HttpClient, that object identifier is 'Client'. The code is below. As I add to the options class, I encounter ConnectionClose and I'm thinking of making it configurable as I want other developers to be able to configure it as desired.

But everything I read about Connection: close in the header indicates I want it keep-alive. Should this value just be false all the time? Or are there valid use cases for the close value of true?

    protected void Setup(ApiCallerOptions options)
    {
        Client = CreateHttpClient();
        Options = options;
        ServicePointManager.FindServicePoint(new Uri(options.BaseAddress))
            .ConnectionLeaseTimeout = options.ConnectionLeaseTimeout;
        Client.BaseAddress = new Uri(options.BaseAddress);
        Client.DefaultRequestHeaders.Accept.Clear();
        Client.DefaultRequestHeaders.Accept.Add(options.ContentType);
        Client.DefaultRequestHeaders.ConnectionClose = false;
    }

Upvotes: 0

Views: 3512

Answers (1)

user6836264
user6836264

Reputation:

RFC https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

14.10 Connection

Can be used for debugging issues related to connections that are kept alive or you simply do not wish to have a persistent connection whatever the reason. These are pretty much the use cases when set explicitly to true HttpClient instance.

To answer your question it should just be false else performance will be degraded.

Upvotes: 2

Related Questions