c# - HttpWebRequest 异常所经历的延迟差异
当 HttpWebRequest 处理 500 服务器错误时,我遇到了一些非常奇怪的行为。
在下面的代码示例中,您可以假设 http://example.com/service.endpoint 立即返回调用时出现 500 服务器错误。
当我在台式电脑(运行 Windows 7 的标准 x86 计算机)上运行此代码时,会引发异常,我将其记录下来,并且该方法立即完成。这是我想要的行为。
当我在开发服务器(独立主机上的 vmware 来宾计算机、运行 Windows Server 2008 R2 Standard 的 Xeon x64)上运行此代码时,会立即引发异常,但该方法需要大约 15 秒才能完成(在引发异常之后) 。
我的问题是 - 是什么导致两次测试之间观察到的行为差异?
(这两种方法的运行条件是相同的 - 即我没有在桌面上运行 Visual Studio 中的代码 - 两者都在相同的服务包装器中运行)。
如果您能提供任何帮助,我们将不胜感激。
void MakeRequest(StringBuilder postParams)
{
var req = (HttpWebRequest)WebRequest.Create("http://example.com/service.endpoint");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.KeepAlive = false;
string postData = postParams.ToString();
req.ContentLength = postData.Length;
req.Timeout = 10000;
Console.WriteLine("Requested started.");
var stOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
stOut.Write(postData);
stOut.Close();
WebResponse response = null;
try
{
response = req.GetResponse();
Stream receiveStream = response.GetResponseStream();
// Deal with response here
response.Close();
Console.WriteLine("Request completed.");
}
catch (Exception ex)
{
if (response != null) response.GetResponseStream().Close();
req.Abort();
Console.WriteLine("Exception with request raised: {0}", ex);
}
// Delay is observed between above "Exception with request.." and following line:
Console.WriteLine("MakeRequest() has completed.");
}
编辑:为了更清楚哪里存在延迟,
此调用所生成的日志的经过严格编辑的版本如下所示。它用于显示在服务器上观察到最大延迟的位置(这在我的桌面上看不到):
2011-08-16 16:39:32,475 [STP OrderPool Thread #0] INFO - Submitting quote to REDACTED
2011-08-16 16:39:32,490 [STP OrderPool Thread #0] INFO - Invoking ASSEMBLY_NAME_REDACTED, Version=1.0.4245.27855, Culture=neutral, PublicKeyToken=null::REDACTED
2011-08-16 16:39:32,709 [STP OrderPool Thread #0] INFO - Sending request to: http://example.com/service.endpoint
2011-08-16 16:39:36,334 [STP OrderPool Thread #0] ERROR - Exception
System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
at System.Net.HttpWebRequest.GetResponse()
at HL.Services.Blackbird.LondonBestxHub.Reuters.Execute.MakeRequest(String serviceName, StringBuilder postParams)
2011-08-16 16:39:57,381 [STP OrderPool Thread #0] INFO - MakeRequest call completed
I'm experiencing some very strange behaviour with a HttpWebRequest when it is handling a 500 server error.
In the code example below, you can assume that http://example.com/service.endpoint immediately returns a 500 server error when it is called.
When I run this code on my desktop PC (a standard x86 machine running Windows 7), the exception gets raised, I log it, and the method immediately completes. This is my desired behaviour.
When I run this code on a development server (a vmware guest machine on a seperate host, Xeon x64 running Windows Server 2008 R2 Standard) the exception is immediately raised, but the method takes around 15 seconds to complete (following the exception being raised).
My question is - what could be causing the difference in behaviour observed between the two tests?
(Both methods the run conditions are the same - i.e. I'm not running the code from Visual Studio on my desktop - both were run within an identical service wrapper).
Any help you can give would be much appreciated.
void MakeRequest(StringBuilder postParams)
{
var req = (HttpWebRequest)WebRequest.Create("http://example.com/service.endpoint");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.KeepAlive = false;
string postData = postParams.ToString();
req.ContentLength = postData.Length;
req.Timeout = 10000;
Console.WriteLine("Requested started.");
var stOut = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
stOut.Write(postData);
stOut.Close();
WebResponse response = null;
try
{
response = req.GetResponse();
Stream receiveStream = response.GetResponseStream();
// Deal with response here
response.Close();
Console.WriteLine("Request completed.");
}
catch (Exception ex)
{
if (response != null) response.GetResponseStream().Close();
req.Abort();
Console.WriteLine("Exception with request raised: {0}", ex);
}
// Delay is observed between above "Exception with request.." and following line:
Console.WriteLine("MakeRequest() has completed.");
}
Edit: To make it clearer where delay exists
A heavily redacted version of the logs this call makes is shown below. It serves to show where the lions share of delay is observed on the server (this is not seen on my desktop):
2011-08-16 16:39:32,475 [STP OrderPool Thread #0] INFO - Submitting quote to REDACTED
2011-08-16 16:39:32,490 [STP OrderPool Thread #0] INFO - Invoking ASSEMBLY_NAME_REDACTED, Version=1.0.4245.27855, Culture=neutral, PublicKeyToken=null::REDACTED
2011-08-16 16:39:32,709 [STP OrderPool Thread #0] INFO - Sending request to: http://example.com/service.endpoint
2011-08-16 16:39:36,334 [STP OrderPool Thread #0] ERROR - Exception
System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
at System.Net.HttpWebRequest.GetResponse()
at HL.Services.Blackbird.LondonBestxHub.Reuters.Execute.MakeRequest(String serviceName, StringBuilder postParams)
2011-08-16 16:39:57,381 [STP OrderPool Thread #0] INFO - MakeRequest call completed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有各种可能性 - DNS 查找、代理等。
我建议您在有问题的计算机上安装 WireShark ,然后查看 < em>到底发生了什么 - 这应该会告诉您问题出在哪里。
(顺便说一句,我强烈建议您对编写器、流、WebResponses 等使用
using
语句。我不认为这是这里的问题,但如果发生什么情况如果您无法关闭WebResponse
,则由于连接池,您可能会因未来对同一主机的请求而超时。)There are various possibilities here - DNS lookups, proxies etc.
I would suggest you install WireShark on the problematic machine, and look at exactly what's going on - that should show you where the problem is.
(As an aside, I would strongly advise you to use
using
statements for writers, streams,WebResponses
etc. I don't think it's the problem here, but if something happens such that you fail to close aWebResponse
, you can end up with timeouts for future requests to the same host due to connection pooling.)