Zaid Amir
Zaid Amir

Reputation: 4785

HttpWebRequest timeout handling

I have a really simple question. I am uploading files to a server using HTTP POST. The thing is I need to specially handle connection timeouts and add a bit of a waiting algorithm after a timeout has occurred to relive the server.

My code is pretty simple:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("SomeURI");
request.Method = "POST";
request.ContentType = "application/octet-stream";
request.KeepAlive = true;
request.Accept = "*/*";
request.Timeout = 300000;
request.AllowWriteStreamBuffering = false;

try
{
      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
      {
            WebHeaderCollection headers = response.Headers;    
            using (Stream Answer = response.GetResponseStream())
            {
                // Handle.
            }
      }
}
catch (WebException e)
{
    if (Timeout_exception)
    {
       //Handle timeout exception
    }
}

I omitted the file reading code as it is not our concern. Now I need to make sure that once a WebException is thrown, I filter the exception to see if it is indeed a timeout exception. I thought of comparing against the exception message yet I am not sure if this is the right way since the application in question is a commercial app and I am afraid that the message varies between different languages. And what message should I be looking for.

Any suggestions?

Upvotes: 27

Views: 57229

Answers (2)

OneLazy
OneLazy

Reputation: 509

Yuval's answer is quite a direct hit but here's a version of mine which I've tried since I've undergone in the same circumstance if you want to target via Status codes:

catch (WebException ex)
            {
                var hwr = (HttpWebResponse)ex.Response;
                if (hwr != null)
                {
                    var responseex = hwr.StatusCode;
                    int statcode = (int)responseex;
                    if (statcode == 404)
                    {
                        Utility.Instance.log(logPath, "The file might not be availble yet at the moment. Please try again later or contact your system administrator.", true);
                    }
                    if (statcode == 401)
                    {
                        Utility.Instance.log(logPath, "Username and Password do not match.", true);
                    }
                    if (statcode == 408)
                    {
                        Utility.Instance.log(logPath, "The operation has timed out", true);
                    }
                }
                else
                {
                    Utility.Instance.log(logPath, ex + ". Please contact your administrator.", true);//Or you can do a different thing here
                }
            }

Upvotes: 0

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149636

You can look at WebException.Status. The WebExceptionStatus enum has a Timeout flag:

try
{
   using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
   {
      WebHeaderCollection headers = response.Headers;    
      using (Stream answer = response.GetResponseStream())
      {
          // Do stuff
      }
   }
}
catch (WebException e)
{
   if (e.Status == WebExceptionStatus.Timeout)
   {
      // Handle timeout exception
   }
   else throw;
}

Using C# 6 exception filters can come in handy here:

try
{
    var request = WebRequest.Create("http://www.google.com");
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        WebHeaderCollection headers = response.Headers;
        using (Stream answer = response.GetResponseStream())
        {
            // Do stuff
        }
    }
}
catch (WebException e) when (e.Status == WebExceptionStatus.Timeout)
{
    // If we got here, it was a timeout exception.
}

Upvotes: 57

Related Questions