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.
Any suggestions for what's going on here?
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.
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.
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.
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.
Use WebRequestHandler class instead of HttpClientHandler and set property HttpWebRequest.UnsafeAuthenticatedConnectionSharing to True.
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