HttpWebRequest timeout handling

50,705

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.
}
Share:
50,705

Related videos on Youtube

Zaid Amir
Author by

Zaid Amir

Updated on March 27, 2020

Comments

  • Zaid Amir
    Zaid Amir about 4 years

    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?