简体   繁体   English

HttpWebRequest - GetResponse() - WebException ReceiveFailure

[英]HttpWebRequest - GetResponse() - WebException ReceiveFailure

I have a WCF service that is running frequent (1000+) outbound connections per minute to external APIs.我有一个 WCF 服务,它每分钟运行频繁(1000+)个到外部 API 的出站连接。

My code throws the following exceptions frequently, but not always showing that is is a WebException with the WebException status property being ReceiveFailure我的代码经常抛出以下异常,但并不总是显示这是一个 WebException,其 WebException 状态属性为ReceiveFailure

The code that is making the outbound request is the following:发出出站请求的代码如下:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(paramBuilder.ToString());
request.ServicePoint.ConnectionLeaseTimeout = 0;
request.Method = "GET";
request.Timeout = 33000;    //33 Second Timeout Is By Design
Stream stream = default(Stream);
HttpWebResponse response = default(HttpWebResponse);
try
{

    response = (HttpWebResponse) request.GetResponse();
    stream = response.GetResponseStream();
    reader = new StreamReader(stream,Encoding.UTF8);
    string str = reader.ReadToEnd();
    return str;

}
catch (WebException exception)
{

    //Handle WebException
}
catch (Exception exception)
{
    //Handle Exception
}
finally
{
    if (reader != null)
        reader.Dispose();

    if (response != null)
        response.Close();

    if (stream != null)
        stream.Dispose();
}

The exception stack trace shows that the exception is caused from GetResponse().异常堆栈跟踪显示异常是由 GetResponse() 引起的。

What could be causing this to happen that I receive an occasional WebException -ReceiveFailure.我偶尔会收到一个 WebException -ReceiveFailure,这可能是什么原因造成的。

I have already reference the MSDN documentation for this status, but that doesn't help me.我已经参考了有关此状态的 MSDN 文档,但这对我没有帮助。

Shooting in the dark here...在这里暗中拍摄...

There is a special condition, while waiting for response: if the system clock is being set automatically by the Windows Time service, or manually, you may experience some unpredictable results.在等待响应时有一个特殊情况:如果系统时钟是由 Windows 时间服务自动设置的,或者是手动设置的,您可能会遇到一些不可预知的结果。

If you're sending your requests over HTTPS, maybe you're facing a regular timeout that was wrongly thrown as a ReceiveFailure.如果您通过 HTTPS 发送您的请求,那么您可能会遇到错误地作为 ReceiveFailure 抛出的常规超时。

Check this article for more information: http://support.microsoft.com/kb/2007873查看这篇文章了解更多信息: http : //support.microsoft.com/kb/2007873

I have a related problem and I realise a few things while I was searching for a solution.我有一个相关的问题,我在寻找解决方案时意识到了一些事情。

  • WebExceptionStatus enum is not equivalent to http status code that the API you call returned. WebExceptionStatus enum不等同于您调用的 API 返回的 http 状态代码。 Instead it is a enum of possible error that may occour during a http call.相反,它是在 http 调用期间可能发生的可能错误的枚举。
  • The WebExceptionStatus error code that will be returned when you receive an error (400 to 599) from your API is WebExceptionStatus.ProtocolError aka number 7 as int.当您从 API 收到错误(400 到 599)时将返回的WebExceptionStatus错误代码是WebExceptionStatus.ProtocolError又名数字 7 作为 int。
  • When you need to get the response body or the real http status code returned from the api, first you need to check if WebException.Status is WebExceptionStatus.ProtocolError .当需要获取api返回的响应体或者真实的http状态码时,首先需要检查WebException.Status是否为WebExceptionStatus.ProtocolError Then you can get the real response from WebExceptionStatus.Response and read its content.然后你可以从WebExceptionStatus.Response获取真正的响应并读取其内容。
  • Sometimes the timeout is handled by the caller (aka your code) so you do not have a response in that case.有时超时由调用者(也就是您的代码)处理,因此在这种情况下您没有响应。 So you can look if WebException.Status is WebExceptionStatus.Timeout所以你可以看看WebException.Status是否是WebExceptionStatus.Timeout

This is an example:这是一个例子:

try
{
    ...
}
catch (WebException webException)
{
    if (webException.Status == WebExceptionStatus.ProtocolError)
    {
        var httpResponse = (HttpWebResponse)webException.Response;
        var responseText = "";
        using (var content = new StreamReader(httpResponse.GetResponseStream()))
        {
            responseText = content.ReadToEnd(); // Get response body as text
        }
        int statusCode = (int)httpResponse.StatusCode; // Get the status code
    }
    else if (webException.Status == WebExceptionStatus.ProtocolError)
    {
       // Timeout handled by your code. You do not have a response here.
    }

    // Handle other webException.Status errors. You do not have a response here.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 哪些特定的状态代码会导致HttpWebRequest.GetResponse()抛出WebException? - Which specific status codes cause a WebException to be thrown by HttpWebRequest.GetResponse()? HttpWebRequest .GetResponse 抛出 WebException '操作已超时' - HttpWebRequest .GetResponse throws WebException 'The operation has timed out' 如果服务器返回200以外的其他值,HttpWebRequest.GetResponse()是否总是抛出WebException吗? - Does HttpWebRequest.GetResponse() always throw a WebException if anything other than 200 is returned by the server? System.Net.WebException:操作在 System.Net.HttpWebRequest.GetResponse 超时 - System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse System.Net.WebException:远程服务器返回错误:(401)未经授权。 在System.Net.HttpWebRequest.GetResponse() - System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse() 当HttpWebRequest.GetResponse抛出WebException时,如何读取返回的自定义错误消息? - How do I read a custom error message returned when HttpWebRequest.GetResponse throws a WebException? C#HttpWebRequest.GetResponse - 如何处理非异常与webexception响应的StatusCode用法? - C# HttpWebRequest.GetResponse - how is StatusCode usage handled for a non-exception vs webexception response? 我正在获取WebException:“操作已超时”立即在HttpWebRequest.GetResponse()上 - I am getting WebException: “The operation has timed out” immediately on HttpWebRequest.GetResponse() WebException getResponse()无法捕获 - WebException getResponse() not catching 从亚马逊 s3 读取图像和视频时,在 System.Net.HttpWebRequest.GetResponse() 处获取 WebException“操作已超时” - Getting WebException “The operation has timed out” at System.Net.HttpWebRequest.GetResponse() when reading images and video from amazon s3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM