Difference between operation has time out and (504) Gateway Timeout

user2711965 picture user2711965 · Sep 6, 2013 · Viewed 35.6k times · Source

I am using HttpWebRequest in my application which is checking some URI's in multiple threads. I am getting multiple types of time out exceptions.

  • The operation has timed out
  • The remote server returned an error: (504) Gateway Timeout.

Their details are like:

System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse() at ......

and

System.Net.WebException: The remote server returned an error: (504) Gateway Timeout. at System.Net.HttpWebRequest.GetResponse() at ....

What is the different between these two.

My function is like:

public bool CheckUri(Uri m_url)
{
    try
    {
        HttpWebRequest request = HttpWebRequest.Create(m_url) as HttpWebRequest;
        request.UserAgent = "MyUserAgent";

        //For: The underlying connection was closed: An unexpected error occurred on a receive.
        request.KeepAlive = false; 
        request.ProtocolVersion = HttpVersion.Version10;

        request.Method = "HEAD"; //Get only the header information 
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            int statusCode = (int)response.StatusCode;
            if (statusCode >= 100 && statusCode < 400) //Good requests
            {
                string sContent = null;
                using (var stream = response.GetResponseStream())
                using (StreamReader loResponseStream = new StreamReader(stream))
                    sContent = loResponseStream.ReadToEnd();
                return true;

            }
            else
            {
                return false;
                //hard to reach here
            }
        }
    }
    //vexing exception
    catch (WebException ex)
    {
        if (ex.Status == WebExceptionStatus.ProtocolError) //400 errors
        {
            var response = ex.Response as HttpWebResponse;

            if (response != null)
            {
                Console.WriteLine("HTTP Status Code: " + (int)response.StatusCode);
                Console.WriteLine(response.StatusCode);
            }
        }
        else
        {
            Console.WriteLine(ex.Message);
        }
        return false;

    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        return false;
    }
}

Also If anyone could tell me, would there be an issue if multiple threads call this method with different URIs. I am not getting any cross thread exception. This method is actually a part of a windows service which monitors a list of almost 200 URIs.

Answer

Michael picture Michael · Sep 15, 2013

In lamest terms...

"Operation Timed Out" means that YOUR program sending the request has timed out waiting for a response. This could mean:

  1. Bad internet connection (if they are all throwing this error then this is likely).
  2. Bad host connection (whomever you are connecting to has a problem).
  3. Bad DNS (if the host is domain name this could be a culprit).
  4. Bad host agent (something on the host end is not responding properly).

In these cases I would manually test connections to the affected host and resolve these issues in that manner. Try testing your own connection first, and other hosts. If the problem is a specific host then they may have issues you need to contact them about.

When you get the "504 - Gateway Timeout" it means that YOUR program did successfully connect to the host, but something went wrong on the host end and it could not return a desired response. This is not a connection problem, but a problem in either the request or the host itself. It could be that the host got stuck in an infinite loop trying to process your request, or is simply "hung", and the agent processing your request gave up and sent your request back.

In these cases I would be looking at the host, maybe running test requests that the host will accept. If the host is not within your control then contact whomever it is and report the error.

So - in short. The first timeout is likely connection related, while the 504 timeout is likely the host processing. Hope this helps.