Keep a http connection alive in C#?

38,320

Solution 1

Have you tried the HttpWebRequest.KeepAlive property? It sets the appropriate Keep-Alive HTTP header and does persist the connections. (Of course this must also be supported and enabled by the remote web server).

The HttpWebRequest.KeepAlive documentation on MSDN states that it is set to true by default for HTTP1.1 connections, so I suspect the server you're trying to contact does not allow connection persistence.

Proxy is used automatically and its settings are taken from your system (read Internet Explorer) settings. It is also possible to override the proxy settings via HttpWebRequest.Proxy property or by tweaking the application configuration file (see http://msdn.microsoft.com/en-us/library/kd3cf2ex.aspx).

Solution 2

Set HttpWebRequest.KeepAlive property True .NET will take care of the rest. It's just database connection pool. Works transparently.

You can create new connection freely .NET will figure it out that you are connecting an already connected server and will use it. Also depends on your Net.ServicePointManager.DefaultConnectionLimit number it will establish new connections (if you do multhithreading).

Solution 3

You can set the HttpWebRequest.KeepAlive property to true.

For the Proxies, there is a property also: HttpWebRequest.Proxy

Solution 4

I had a similar issue when HttpWebRequest.KeepAlive = true was not enough to keep it alive. The connection stayed on only after I set request.UnsafeAuthenticatedConnectionSharing = true and ServicePointManager.MaxServicePointIdleTime = 100000;//avoid 0 or low values

Solution 5

If you're using Http 1.1 you shouldn't be using Keep-Alive as the connection is implicitly Keep-Alive.

The HttpWebRequest.KeepAlive property can be set but if you're sending an Http 1.1 request it will not actually set the header unless you set it to Close.

Here's another question that has more info: is an HTTP/1.1 request implicitly keep-alive by default?

Share:
38,320
Admin
Author by

Admin

Updated on May 15, 2020

Comments

  • Admin
    Admin almost 4 years

    How do i keep a connection alive in C#? I'm not doing it right. Am i suppose to create an HttpWebRequest obj and use it to go to any URLs i need? i dont see a way to visit a url other then the HttpWebRequest.Create static method.

    How do i create a connection, keep it alive, browse multiple pages/media on the page and support proxys? (i hear proxy are easy and support is almost standard?) -edit- good answers. How do i request a 2nd url?

    {
    HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create("http://google.com");
    WebRequestObject.KeepAlive = true;
    //do stuff
    WebRequestObject.Something("http://www.google.com/intl/en_ALL/images/logo.gif");
    }
    
  • Timothy Gonzalez
    Timothy Gonzalez almost 7 years
    I don't find this to be exactly true. .NET needs help in managing the connections by reusing an HttpClient instance. I'm working through an issue now where there are thousands of Established unused connections, because keep-alive is used with a -1 connection lease timeout and a new instance of HttpClient be used per request. This may just be an issue with the newer HttpClient class though.