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:

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

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:

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

Read more from the Uncategorized category. If you would like to leave a comment, click here: 5 Comments. or stay up to date with this post via RSS, or you can Trackback from your site.
Social Bookmark : Technorati, Digg, de.licio.us, Yahoo, Blinkbits, Blogmarks, Google, Magnolia.

Leave a Comment

Name (required)

Email (required)

Website

Comments

5 Comments so far

  1. Édouard Mercier October 11, 2008 7:45 pm

    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 November 22, 2008 3:08 am

    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 December 2, 2008 3:05 am

    Thanks!

  4. Will May 22, 2009 5:18 pm

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

  5. [...] [...]