[英]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.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.WebExceptionStatus
错误代码是WebExceptionStatus.ProtocolError
又名数字 7 作为 int。WebException.Status
is WebExceptionStatus.ProtocolError
.WebException.Status
是否为WebExceptionStatus.ProtocolError
。 Then you can get the real response from WebExceptionStatus.Response
and read its content.WebExceptionStatus.Response
获取真正的响应并读取其内容。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.