1

I am struggling to achieve a requirement from the client. I have a web service which in turn invokes client's webservice. Our service POST a request to the client service using UploadString() property of webclient.Here I have frequent requests to the client service,so I would like to keep the connection alive.I came to know that in HTTP 1.1 there is a feature called "KeepAlive" which allows persistent connection.My question is, is it possible to configure the timeout property of this keepalive via c# code?

Appreciate your help:)

Sudheesh
  • 13
  • 1
  • 4
  • Related : https://msdn.microsoft.com/en-us/library/system.net.servicepoint.settcpkeepalive(v=vs.110).aspx –  Jul 22 '16 at 13:12
  • @x... Thanks for your reply.But Can you please let me know how I can achieve it using web client?Sorry I am not an expert in this area. – Sudheesh Jul 22 '16 at 13:24

1 Answers1

0

To use that method, you must create a class derived from WebClient, see this example. You can set timeout, keepalive, tcpkeepalive also there. And use this class, instead of WebClient:

  public class WebClientExtended : WebClient
  {
    protected override WebRequest GetWebRequest(Uri uri)
    {
      var w = (HttpWebRequest) base.GetWebRequest(uri);
      w.Timeout = 5000;      // Set timeout
      w.KeepAlive = true;    // Set keepalive true or false
      w.ServicePoint.SetTcpKeepAlive(true, 1000, 5000);  // Set tcp keepalive
      return w;
    }
  }
  • Thanks @x... Seems it worked.Can you please also let me know what is the default timeout for keep alive?Also whether it is possible to decrease further to this default value using the above code? – Sudheesh Jul 27 '16 at 08:09
  • https://blogs.technet.microsoft.com/nettracer/2010/06/03/things-that-you-may-want-to-know-about-tcp-keepalives/ –  Jul 27 '16 at 08:39
  • @X... Is there any way to test the keepalive is working as expected? – Sudheesh Aug 26 '16 at 07:27