操作之间的差异有时间出来,(504)网关超时网关、差异、操作、时间

2023-09-04 02:35:28 作者:捂著心脏、逞強旳笑

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

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.

在该操作已超时 在远程服务器返回错误:(504)网关超时

他们的细节是这样的:

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

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

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

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.

我的功能是这样的:

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;
    }
}

此外,如果有人可以告诉我,会不会有一个问题,如果多个线程调用此方法具有不同的URI。我没有收到任何交叉线程例外。这种方法实际上是一个窗口服务,监控近200 URI列表的一部分。

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.

推荐答案

在最烂的方面...

操作超时意味着你的程序发送请求已超时等待响应。这可能意味着:

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

在坏的互联网连接(如果它们都抛出这个错误,那么这是有可能的)。 坏主机连接(何人所要连接到有问题)。 在不正确的DNS(如果主机域名,这可能是罪魁祸首)。 为主机代理(在主机端的东西没有正确响应)。

在这种情况下,我将手动测试连接到受影响的主机和解决的方式处理这些问题。尝试测试自己的连接第一,和其他主机。如果问题是一个特定的主机,然后他们可能需要与他们联系有关的问题。

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.

当你得到了504 - 网关超时这意味着你的程序并成功地连接到主机,但出事了在主机端,它不能返回所需的响应。这不是一个连接的问题,但在任一请求或主机本身的问题。这可能是因为主机陷在一个无限循环尝试处理您的请求,或者是简单的挂起,并且代理处理您的请求放弃了,送你的要求了。

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.

所以 - 简而言之。第一超时很可能是连接相关,而504超时是有可能的主机处理。希望这有助于。

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