HttpWebRequest timeout not working

Occasional Visitor


When I disconnect the ethernet, the request just hangs.




                    HttpWebRequest request = (HttpWebRequest )WebRequest.Create(WebAddressPreAmble + WebAddress + WebAddressQuery); // Create(sQuery);
                    request.Credentials = CredentialCache.DefaultCredentials;
                    request.Timeout = 10000;
                    request.ReadWriteTimeout = 1000;
                    // Get the response.
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    // Display the status.
                    if (response.StatusDescription.Equals("OK"))



1 Reply
You have no exception handling so it might be throwing an error and the code hanging elsewhere.

HttpWebRequest can return a WebException which is not caught as a normal exception. Also, when using external resources you should always have try/catch blocks.

// web request code
catch (WebException e)
if (e.Status == WebExceptionStatus.Timeout)
// handle timeout
else throw; // optional
catch (Exception ex)
// handle exception

Some links of the above: