Why WebRequest timeout always runs on first request, but never on any subsequent request

There was a problem when the call to WebRequest.GetResponse() hangs and expires on the first call, but after the first call everything works fine.

  try { WebRequest myHttpWebRequest = WebRequest.Create(@"http://192.168.xx/"); // Sends the HttpWebRequest and waits for the response. myHttpWebRequest.Timeout = 1000; WebResponse myHttpWebResponse = myHttpWebRequest.GetResponse(); } catch(Exception e) { Console.WriteLine("Failure 1"); } try { WebRequest myHttpWebRequest = WebRequest.Create(@"http://192.168.xx/"); // Sends the HttpWebRequest and waits for the response. myHttpWebRequest.Timeout = 1000; WebResponse myHttpWebResponse = myHttpWebRequest.GetResponse(); } catch(Exception e) { Console.WriteLine("Failure 2"); } try { WebRequest myHttpWebRequest = WebRequest.Create(@"http://192.168.xx/"); // Sends the HttpWebRequest and waits for the response. myHttpWebRequest.Timeout = 1000; WebResponse myHttpWebResponse = myHttpWebRequest.GetResponse(); } catch(Exception e) { Console.WriteLine("Failure 3"); } 

using this code in a console application, I always get Failure 1 . Running under the debugger or not. I made a cycle of 1000 , and it always fails on the first and not on the others. In fact, while reading the web server logs, it never actually receives the first request. Did I miss something?

+7
source share
4 answers

EDIT: I realized that the answer below corresponds to the exact opposite situation when the first request works and the others do not. However, this is still important - you really have to get rid of your answers. It would also be useful if you also report an error message when reporting an error ...

In order to understand what is happening here, you should really use something like WireShark so that you can see if the problem is that the request is being made, but is not responding to it, or not even being done.

I wonder if the problem really is that it resolves the proxy server or something like that ... and enough time to resolve it before the second request expires. Try to increase timeouts. Again, this should be visible through WireShark.


You do not get rid of the answer on the Internet, so the connection pool for the second request is timed out, waiting for the connection to return.

Put the WebResponse part in the using statement, and you will probably find that everything is working fine:

 using (WebResponse myHttpWebResponse = myHttpWebRequest.GetResponse()) { } 

This suggests that you are really doing something with the answer, of course. Otherwise, you could simply write:

 myHttpWebRequest.GetResponse().Dispose(); 

:)

+9
source

Maybe a little late, but I had exactly the same effect. Finally, the reason was that the network did not have a default gateway. The solution was to optianally set request.Proxy = null .

 var request = WebRequest.Create(UriString); request.Timeout = Timeout; if (_disableProxy) { request.Proxy = null; } if (request is HttpWebRequest) { var response = (HttpWebResponse)request.GetResponse(); responseStream = response.GetResponseStream(); } if (request is FtpWebRequest) { var response = (FtpWebResponse)request.GetResponse(); responseStream = response.GetResponseStream(); } else if (request is FileWebRequest) { var response = (FileWebResponse)request.GetResponse(); responseStream = response.GetResponseStream(); } 

Hope this helps.

+3
source

You can get behavior very similar to this if you did not reset \ close RequestStream before requesting a response. This behavior seems to exist on .NET 3.5, but was discussed in the .NET Framework 4.5. I noticed a problem when switching frameworks - the code (without closing), which worked in 4.5, stopped working when compiling with 3.5. Perhaps try to explicitly get a RequestStream and close it as a workaround.

0
source

I ran into the same problem, in my case I increased the timeout value of the WebRequest object and it will work!

 webRequest.Timeout = int.Parse(60000); 

(I set the timeout property to 60 seconds).

0
source

All Articles