Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse HTTP connection across multiple requests?

I'm making an application which constantly scans remote website for any changes. I'm using System.Net.Http.HttpClient, but I noticed that it probably doesn't support reusing connection.

Requests are executed one after one, but normally they take about 250 ms to be completed. When I have Fiddler turned on with "Reuse server connections" option on, it drops to as low as 150 ms per request.

I guess I have misconfigured HttpClient, but I can't find any information in MSDN reference which would help me to solve the problem.

like image 802
stil Avatar asked Nov 07 '14 11:11

stil


People also ask

How do I keep my HTTP connection alive?

Keep-Alive is enabled by explicitly requesting it via HTTP header. If you don't have access to your web server configuration file, you can add HTTP headers yourself by using . htaccess file.

Is every HTTP request a new TCP connection?

HTTP mostly relies on TCP for its transport protocol, providing a connection between the client and the server. In its infancy, HTTP used a single model to handle such connections. These connections were short-lived: a new one created each time a request needed sending, and closed once the answer had been received.

Are TCP connections reused?

The HTTP underlying tcp connection is not reused even though the requests are made to the same host. This is due to the following reason, specifically with the way we use the response body in our Go HTTP response.

Is HTTP connection persistent?

HTTP has a persistent connection function that allows the channel to remain open rather than be closed after a requested exchange of data. TCP is a connection-oriented protocol: It starts a connection after confirmation from both ends that they are available and open to a data exchange.


1 Answers

For a HttpWebRequest, you would need to set the KeepAlive property.

HttpClient doesn't appear to offer a similar property, but it probably attempts to use keepalive by default. What are the request and response headers? (HTTP/1.0 only supports keepalive if the server explicitly sends a Connection: Keep-alive response header.

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

like image 166
EricLaw Avatar answered Oct 28 '22 18:10

EricLaw