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
}



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
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.
Thanks!
Good god this has been bugging me for days. Thanks for documenting it. Close() completely sorts it out!
Man, I love you!!
Thanks for posting this! It worked like a charm for me!
Thanks for sharing this, it saved me as well, had me scratching my head for a while!
A solution to our problem! Thanks Johan