Re: HttpWebRequest.GetRequestStream times out




"Joerg Jooss" <news-reply@xxxxxxxxxxxxx> wrote in message
news:94fc5071328f68c858a01d1affb8@xxxxxxxxxxxxxxxxxxxxxxx
Thus wrote Christer,

Hello.
Hope this is the right group to post to. If not, please let me know
where to
post.
I'm having a problem doing an HTTP POST to my payment provider during
load. During days with low load, no errors occur.

The code being executed is in a class library loaded by an ASP.NET
application.
The server machine is a 2003 Server Std Ed. w/SP1, Intel Xeon 2.8 GHz
with
2.5 GB RAM.
.NET version is 1.1. The mscorlib.dll has version 1.1.4322.2300, I
take it
that it means .NET SP1...
When I encounter this exception, this is printed in the log:

Payment Authorization failed (WebException): Timeout. Message: The
operation has timed-out.. Response: . Stack Trace: at
System.Net.HttpWebRequest.GetRequestStream()

at Payment.DoPaymentAuth()

I have tried to configure the machine.config file according to the
reccomendations at MSDN:
- Set maxConnection to 48 (12 * 4)
- Set maxWorkerThreads to 100
- Set minFreeThreads to 352 (88* 4)
How can I investigate this further?

I hope someone can help me with this.

Here is the code:

<code>

try
{
//The postString has value "cardno=XXX&expmon=06&expyear=06" etc.
byte[] sendData = Encoding.ASCII.GetBytes(postString);
int sendLength = sendData.Length;
HttpWebRequest httpReq = (HttpWebRequest)
WebRequest.Create(PAYMENT_AUTH_URI);

httpReq.Method = "POST";
httpReq.ContentType="application/x-www-form-urlencoded";
httpReq.ContentLength = sendLength;
httpReq.ConnectionGroupName = "PaymentGroup";

//Error comes here...
Stream sendStream = httpReq.GetRequestStream();
sendStream.Write(sendData,0,sendLength);
sendStream.Flush();
sendStream.Close();
sendStream = null;
WebResponse resp = httpReq.GetResponse();
Stream respstrm = resp.GetResponseStream();
int length = 1024;
byte[] Buffer = new byte[length];
int bytesRead = 0;
bytesRead = respstrm.Read(Buffer,0,length);
if(bytesRead > 0 )
{
postReturn = Encoding.ASCII.GetString(Buffer,0, bytesRead);
}
try
{
respstrm.Flush();
respstrm.Close();
respstrm = null;
}
catch(Exception e)
{
respstrm = null;
}
try
{
resp.Close();
resp = null;
}
catch(Exception e)
{
resp = null;
}
}
catch(System.Net.WebException we)
{
LogError("Order Id " + this.OrderId + ": Payment Authorization
failed
(WebException)(" +
DateTime.Now.ToString(dateformat) + "): " + we.Status.ToString()
+ ".
Message: " +
we.Message + ". Response: " + we.Response + ". Stack Trace: " +
we.StackTrace);
if(we.Status == WebExceptionStatus.Timeout)
{
postReturn = "Timeout";
}
else
{
postReturn = "Error";
}
}
catch(Exception ex)
{
postReturn = "Error";
}
return postReturn;

</code>

Thanks!

Save for the somewhat scary exception handling (sorry!) and the fact that
you never read more than 1 kB of the response, the code should work. The
question is: What times out? Writing to the request stream? Obtaining the
response? Reading from the response stream? Can you recreate the failure
during a load test?
Cheers,
--
Joerg Jooss
news-reply@xxxxxxxxxxxxx



Hi Joerg, thanks for your reply.

I know... the code could be way better.
The reason I haven't stressed a rewrite of the code is that this error never
occurs in our test environment. I use MS ACT to test, and 200 simultaneous
users never produce this exception.
I am able to reproduce the exception in a load test in the production
environment. It happes with only 50 simultaneous users. (Also, the response
is never larger than about 500 bytes, so there is no need to use a larger
buffer.) I even tried testing code that does everything inside try/finally
blocks, closing and disposing anyything that has a Close() or Dispose()
method. Same result.

It is the call to GetRequestStream() that times out. All I get in the stack
trace is what I posted earlier.

The request timeout is set to 100 seconds, and the requests that time out
throw this exception exactly 100 seconds after the call to
GetRequestStream() was issued. Requests just before and after usually
complete within a second.

The hardware running the test solution is way smaller than the prod
solution, and the IIS even hosts 10- 15 webs. It cannot be a hardware issue.

What is actually happening when you call GetRequestStream()? Is a DNS lookup
performed, or is that done earlier? I am starting to suspect that the
network is a bottle neck. But, the NIC on the machine is a 1 gbps card.

I was thinking I should try editing the hosts file to maybe avoid doing a
DNS resolve, but I haven't gotten around to it yet.

Any suggestions?
I hope you can help me some more with this.
Thanks!

C.





.



Relevant Pages

  • Event sink registration via WebDAV
    ... The exception: "The remote server returned an error Method not allowed" at line 68. ... string myExchange = "http://exdevel2k3/exchange/";; ... // Set the request timeout to 5 minutes ... Stream QueryStream = HttpWRequest.GetRequestStream; ...
    (microsoft.public.exchange2000.development)
  • Re: problem WebRequest and WebResponse
    ... the Stream and the WebResponse instances. ... HttpWebRequest request = ... WebResponse response = null; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: NotificationSampleWebDav-Monitor OWA Inbox
    ... re-login or update your cookie with each response you get... ... Warning: Exiting Action with an exception: The remote server returned an ... // Create request object and assign credentials. ... Stream newStream = Request.GetRequestStream; ...
    (microsoft.public.exchange.applications)
  • Re: Remote host closed the connection error - .Net 2
    ... No uploads always when writing a response to the client so request length ... internal exception when flushing data out to the response stream. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: WebDAV mark as read?
    ... I am using following code to change the message read/unread status. ... // Add the network credentials to the request. ... Stream QueryStream = HttpWRequest.GetRequestStream; ... // Send Request and Get Response ...
    (microsoft.public.exchange.applications)