C# 操作超时和 (504) 网关超时之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18665756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:53:08  来源:igfitidea点击:

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

c#.netsystem.net.webexception

提问by user2711965

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

HttpWebRequest在我的应用程序中使用它检查多个线程中的一些 URI。我收到多种类型的超时异常。

  • The operation has timed out
  • The remote server returned an error: (504) Gateway Timeout.
  • 操作已超时
  • 远程服务器返回错误:(504) 网关超时。

Their details are like:

他们的细节是这样的:

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

System.Net.WebException:操作在 System.Net.HttpWebRequest.GetResponse() 处超时......

and

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

System.Net.WebException:远程服务器返回错误:(504) 网关超时。在 System.Net.HttpWebRequest.GetResponse() 在 ....

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.

另外,如果有人能告诉我,如果多个线程使用不同的 URI 调用此方法,会不会出现问题。我没有收到任何跨线程异常。该方法实际上是 Windows 服务的一部分,该服务监视近 200 个 URI 的列表。

采纳答案by Michael

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).
  1. 互联网连接不好(如果他们都抛出这个错误,那么这很可能)。
  2. 错误的主机连接(无论您连接到谁都有问题)。
  3. DNS 错误(如果主机是域名,这可能是罪魁祸首)。
  4. 主机代理错误(主机端的某些内容未正确响应)。

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.

当您收到“504 - Gateway Timeout”时,这意味着您的程序确实成功连接到主机,但主机端出现问题并且无法返回所需的响应。这不是连接问题,而是请求或主机本身的问题。可能是主机在尝试处理您的请求时陷入无限循环,或者只是“挂起”,而处理您的请求的代理放弃并将您的请求发回。

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.

所以 - 简而言之。第一次超时可能与连接有关,而 504 超时可能与主机处理有关。希望这可以帮助。

回答by Sumit

You can consult this linkfor "The operation has timed out" and the issue might be due to the server already busy with some task. And (504) Gateway Timeout means that one server did not receive a timely response from another server that it was accessing while attempting to load the web page or fill another request by the browser.

您可以查阅此链接以了解“操作已超时”,问题可能是由于服务器已经忙于处理某些任务。并且 (504) 网关超时意味着一个服务器在尝试加载网页或填充浏览器的另一个请求时没有收到来自它正在访问的另一台服务器的及时响应。

回答by Ajay Kumar

The operation has timed outoccurs when a specified time is defined and server is not able to respond back in that particular time (occurs inside the remote server)

当定义了指定时间并且服务器无法在该特定时间响应(发生在远程服务器内部),操作已超时

while

尽管

504 errorsin the HTTP cycle (occurs in communication between client and server)Any client (e.g. your Web browser or our CheckUpDown robot) goes through the following cycle when it communicates with the Web server:

HTTP 循环中的 504 错误(发生在客户端和服务器之间的通信中)任何客户端(例如您的 Web 浏览器或我们的 CheckUpDown 机器人)在与 Web 服务器通信时都会经历以下循环:

Obtain an IP address from the IP name of the site (the site URL without the leading 'http://'). This lookup (conversion of IP name to IP address) is provided by domain name servers (DNSs). Open an IP socket connection to that IP address. Write an HTTP data stream through that socket. Receive an HTTP data stream back from the Web server in response. This data stream contains status codes whose values are determined by the HTTP protocol. Parse this data stream for status codes and other useful information. This error occurs in the final step above when the client receives an HTTP status code that it recognises as '504'. (Last updated: March 2012).

从站点的 IP 名称(不带前导“http://”的站点 URL)获取 IP 地址。这种查找(IP 名称到 IP 地址的转换)由域名服务器 (DNS) 提供。打开与该 IP 地址的 IP 套接字连接。通过该套接字写入 HTTP 数据流。从 Web 服务器接收 HTTP 数据流作为响应。该数据流包含状态代码,其值由 HTTP 协议确定。解析此数据流以获取状态代码和其他有用信息。当客户端收到它识别为“504”的 HTTP 状态代码时,会在上述最后一步中发生此错误。(上次更新时间:2012 年 3 月)。

Fixing 504 errors - general

修复 504 错误 - 一般

This problem is entirely due to slow IP communication between back-end computers, possibly including the Web server. Only the people who set up the network at the site which hosts the Web server can fix this problem.

此问题完全是由于后端计算机(可能包括 Web 服务器)之间的 IP 通信缓慢所致。只有在托管 Web 服务器的站点上设置网络的人员才能解决此问题。

回答by Athari

The operation has timed outis a clienterror. It is usually caused by various *Timeoutproperties 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.

操作超时客户端错误。它通常是由(及其后代)的各种*Timeout属性引起的WebRequestTimeout, ContinueTimeout, ReadWriteTimeout。如果您发送请求的服务器在您设置的超时时间内没有响应,您将获得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 Timeoutis a servererror. It is usually caused by errors in or overloading of the server infrastructure you sent requests to. It is black box for you.

504 网关超时服务器错误。它通常是由您向其发送请求的服务器基础结构中的错误或过载引起的。这是你的黑匣子。

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:

通常,如果您没有获得 HTTP 代码,则它是 .NET 的一个例外。如果您确实获得了 HTTP 代码,则可以查看第一个数字:

2**OK
3**Redirection
4**Clienterror
5**Servererror

2**OK
3**重定向客户端错误服务器错误
4**
5**