HttpWebRequest on Compact Framework: The operation has timed-out

With .Net Compact Framework I developed an application that is doing a webrequest on interval bases. With the webrequest I sent data by providing some arguments inside the querystring. I am not interested in the response and therefore I did not assign the GetResponse() return value. I use the following code:

HttpWebRequest webRequest;
try
{
   webRequest = WebRequest.Create("http://someurl.com/with.aspx?querystring=value");
   webRequest.Method = "GET";
   webRequest.KeepAlive = false;
   webRequest.GetResponse();
}
catch (Exception e){
    //Something goes wrong. Implement handling of this exception
}
finally {
   webRequest = null; //To clear up the webrequest
}

This code works… for only 2 times. The third time it is executed I get a: The operation has timed-out exception. It looks like the GetResponse() method keeps a connection (or socket) open which make it not possible to make new connection at the third time the method is executed. I solved this problem by assigning the GetResponse() value to a WebRequest variable and execute the Close() method inside the finally part of the try catch:

HttpWebRequest webRequest;
WebResponse webResponse = null; //Need to assign this as null otherwise it does not compile
try
{
   webRequest = WebRequest.Create("http://someurl.com/with.aspx?querystring=value");
   webRequest.Method = "GET";
   webRequest.KeepAlive = false;
   webResponse = webRequest.GetResponse(); //Assign to webResponse
}
catch (Exception e){
    //Something goes wrong. Implement handling of this exception
}
finally {
   if(webResponse != null)
   {
      webResponse.Close(); //Close webresponse connection
   }
   webResponse = null; //To clear up the webresponse
   webRequest = null; //To clear up the webrequest
}

7 Comments

  1. Had the same problem, and your work-around saved me. Thanks a lot. It seems as if the underlying win32 socket is not closed properly otherwise. Cheers, Édouard

  2. Salvatore says:

    You're a genius... I searched that bloody close method in HttpWebRequest, but I did not look also into HttpWebResponse!
    BTW, sometimes you get timeouts also because the server-side blocks TCP connection from the device. (Microsoft claims this is a tcpip.sys protection against DoS attacks... other people say it is an emule-rate-limiter!). You can download a patch for tcpip.sys to fix this.

  3. Stephen Strenn says:

    Thanks!

  4. Will says:

    Good god this has been bugging me for days. Thanks for documenting it. Close() completely sorts it out!

  5. Stephane says:

    Man, I love you!!
    Thanks for posting this! It worked like a charm for me!

  6. Gary Wright says:

    Thanks for sharing this, it saved me as well, had me scratching my head for a while!

  7. Barry says:

    A solution to our problem! Thanks Johan :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>