Forum Discussion
Boyd1_Mills
Nov 09, 2021Copper Contributor
HttpWebRequest timeout not working
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
Sort By
- darren_kCopper ContributorYou 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.
try
{
// 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:
https://docs.microsoft.com/en-us/dotnet/api/system.net.webexceptionstatus?view=net-5.0
https://docs.microsoft.com/en-us/dotnet/framework/network-programming/handling-errors