Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection keep-alive not working with System.Net.Http.HttpClient on certain hosts

I'm experimenting with the Heroku API using the .NET System.Net.Http.HttpClient. In particular, I want keep-alive to work so that I can send many HTTP requests using one TCP connection and only do one SSL handshake instead of setting up a TCP connection with SSL handshakes per request.

I'm testing against https://api.heroku.com/status which gives a nice HTTP 200, and using Wireshark to monitor TCP traffic.

Google Chrome, ApacheBench (with -k flag) and curl all seem to be able to keep a TCP connection open and send multiple requests. HttpClient, not so much. I have also tested HttpClient against other hosts (eg. https://google.com/ and there I only see one SSL handshake/TCP setup. So it seems like it's a bad interaction between HttpClient and the Heroku API.

Here's the code I'm testing with:

private static async void TestUrl(string url)
{
    using (var client = GetClient())
    {
        await client.GetAsync(url);
        await client.GetAsync(url);
        await client.GetAsync(url);
        await client.GetAsync(url);
    }
}

private static HttpClient GetClient()
{
    var requestHandler = new HttpClientHandler
    {
        UseCookies = false,
        AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
    };

    return new HttpClient(requestHandler);
}

I'm also attaching a Wireshark screenshot showing how HttpClient decides to reset (RST) the connection as soon as it's done receiving data.

Wireshark screenshot

Any suggestions for what's going on here?

like image 853
friism Avatar asked Jul 27 '13 05:07

friism


People also ask

How do I test http keep-alive?

In order to check if your pages are delivered with a Keep-Alive header, you can use the HTTP Header Checker tool. This will display the Connection: Keep-Alive field if the HTTP Keep-Alive header is enabled.

What is client side Keep-Alive?

The Client Keep-Alive mode enables the Citrix ADC appliance to process multiple requests and responses using the same socket connection. The feature keeps the connection between the client and the appliance (client-side connection) open even after the server closes the connection with the appliance.

How does connection keep-alive work?

Keep-Alive, also known as a persistent connection, is a communication pattern between a server and a client to reduce the HTTP request amount and speed up a web page. When Keep-Alive is turned on, the client and the server agree to keep the connection for subsequent requests or responses open.

What is the purpose of the Keep-Alive message?

A keepalive (KA) is a message sent by one device to another to check that the link between the two is operating, or to prevent the link from being broken.


1 Answers

Use WebRequestHandler class instead of HttpClientHandler and set property HttpWebRequest.UnsafeAuthenticatedConnectionSharing to True.

like image 142
Pro100Oleh Avatar answered Sep 27 '22 19:09

Pro100Oleh