Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 249
user2711965 Avatar asked Sep 06 '13 20:09

user2711965


2 Answers

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.

like image 123
Michael Avatar answered Sep 30 '22 17:09

Michael


The operation has timed out is a client error. It is usually caused by various *Timeout properties of WebRequest (and its descendants): Timeout, ContinueTimeout, ReadWriteTimeout. If the server you sent request to does not respond within the timeout you set, you get TimeoutException.

To avoid this error, you can increase timeouts. However, they are quite big by default, so increasing is unlikely to help. You can try several times. If it doesn't help, the server is probably down.

504 Gateway Timeout is a server error. It is usually caused by errors in or overloading of the server infrastructure you sent requests to. It is black box for you.

You can't do anything about this error, only server administrators can resolve that. If the error is caused by overloading, you can try requesting several times, but doing this too often will do more harm than good, obviously.

In general, if you don't get an HTTP code, it's an exception from .NET. If you do get an HTTP code, you can look at the first digit:

2** OK
3** Redirection
4** Client error
5** Server error

like image 30
Athari Avatar answered Sep 30 '22 15:09

Athari