c# - Differences in delays experienced with HttpWebRequest exceptions
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
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 a WebResponse
, you can end up with timeouts for future requests to the same host due to connection pooling.)
精彩评论