Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HttpWebResponse's GetResponse() hangs and timeouts

1,944 views
Skip to first unread message

Nathan

unread,
Sep 24, 2003, 7:05:00 PM9/24/03
to
This is a copy of a message at microsoft.public.dotnet.framework.clr:

THE CODE:

I'm using an HttpWebResponse object to send an HTTP POST to a Java server I have written and are running on the same machine (for dev and testing).  Here is the C# code snippet:

1  string clientAddr = "http://127.0.0.1:22225/";
2  try
3  {
4    webreq = (HttpWebRequest)WebRequest.Create( clientAddr );
5    //webreq.Proxy = GlobalProxySelection.GetEmptyWebProxy();
6    webreq.Method="POST";
7    ASCIIEncoding encoding = new ASCIIEncoding();
8    byte[] outGoingBytes = encoding.GetBytes( _msg );
9    webreq.ContentType = "application/x-www-form-urlencoded";
10   //webreq.KeepAlive = false;
11   // Set the content length of the string being posted.
12   webreq.ContentLength = outGoingBytes.Length;
13   //webreq.AllowWriteStreamBuffering = false;
14   //webreq.Timeout = 15000;
15   Stream outGoingStream = webreq.GetRequestStream();
16   outGoingStream.Write(outGoingBytes, 0 , outGoingBytes.Length);
17   outGoingStream.Flush();
18   outGoingStream.Close();
 
19   webresp = (HttpWebResponse)webreq.GetResponse();
20   Stream streamResponse = webresp.GetResponseStream();
21   Encoding UTF8Encoding = System.Text.Encoding.GetEncoding("utf-8");
22   StreamReader streamRead = new StreamReader( streamResponse, UTF8Encoding );
23   Char[] readBuff = new Char[256];
24   int count = streamRead.Read( readBuff, 0, 256 );
25   Console.WriteLine("The return stream is: "); 
26   while (count > 0)
27   {
28     String outputData = new String(readBuff, 0, count);
29     Console.Write(outputData);
30     count = streamRead.Read(readBuff, 0, 256);
31   }
32   Console.WriteLine();
33   // Close the Stream object.
34   streamResponse.Close();
35   streamRead.Close();
36   // Release the resources held by response object.
37   webresp.Close();
38 }
39 catch( System.Net.WebException we )
40 {
41   Console.WriteLine( "Error with client [" + clientAddr + "]: " +
42                      we.ToString() + "\n" + we.StackTrace );
43   if( webresp != null )
44     webresp.Close();
45 }

[Line numbers changed for simplicity]

PROBLEM DESCRIPTION:

The C# code should create an HTTP Header and POST with _msg at Line 8 as the content and send it to port 22225 on the same machine.  What happens is at Line 16 the HttpWebRequest creates the socket and sends ONLY the HTTP Header and HTTP Delimiter ( "\r\n\r\n" ).  The Java server gets the Header and the Header's delimiter.  Then, the C# program proceeds to line 19 and hangs.  The Java server blocks on it's socket during the hang.  C# hangs until the socket timeout is reached and then triggers the following message:

Error with sending a message to client [http://127.0.0.1:22225/]: System.Net.WebException: The operation has timed-out.
   at System.Net.HttpWebRequest.GetResponse()
   at Msg4Client.processMessage() in \...\clientsrv.cs:line 19
   at System.Net.HttpWebRequest.GetResponse()
   at Msg4Client.processMessage() in \...\clientsrv.cs:line 19

At this exact moment, the Java server I wrote gets the body of the POST (contents of _msg) in it's entirity.  But, the C# program's thread is now dead and can't receive a response from the Java server because of the exception and lack of webresp being returned on line 19.

Does anyone know why the GetResponse() is hanging and then sends the POST's body at the timeout?  Why is the GetResponse WebException being cited twice in the call stack?  This is a multithreaded call, but at the time of testing there is only one Msg4Client thread.

SOLUTIONS WHICH DON'T WORK

I've tried the following things which have not worked.  1) I played with the proxy setting at Line 5.  This connection shouldn't even have to use a proxy since it's on the same system.  But setting no proxy or using the default proxy doesn't affect this problem.  2) I played with the KeepAlive setting at Line 10, but both true and false still show this problem. 3) AllowWriteStreamBuffering at Line 13 has no affect either true or false. 4) Setting the timeout on Line 14 only lengths or shortens the hang period - that is all.  5) Inserting a System.GC.Collect() above Line 19 doesn't help either.

ISN'T THE JAVA AT FAULT?

It's obviously possible the Java server is the problem, but I don't think so.  Here's why: I'm using a ServerSocket to listen to port 22225 and it accepts the C#'s connection just fine.  It also get's the header and header delimiter from C# just fine, but if I read in from the DataInputStream in bytes or BufferedReader in readLines they both block on the socket until C# hit's a timeout and still returns the POST's entire contents only after that timeout.  I think that the C# doesn't acutally send it's content until the timeout is triggered, because Java blocks on the socket until that timeout happens even reading byte by byte, then it returns the data after C#'s timeout.

Bizarre.  Any help would be appreciated!

-Nathan

Trebek

unread,
Sep 24, 2003, 9:24:56 PM9/24/03
to
Nathan ,

Just prior to instantiating the web request, try adding the following line:

int tmp = ServicePointManager.DefaultConnectionLimit
This will force a load of the config files and should prevent the socket
from hanging. This 'type' of problem is a known issue with MS C# sockets.
If you haven't considered it already, I would suggest looking at the
TCPClient class. I had a similar issue and got around the problem with the
above solution.

Alex


"Nathan" <goo...@waitefamily.com> wrote in message
news:vn48ots...@corp.supernews.com...

Nathan

unread,
Sep 25, 2003, 1:04:13 PM9/25/03
to
Hi Alex,

Thanks for playing Jeopardy with me ;-) I put "int tmp =
ServicePointManager.DefaultConnectionLimit" at Line 2 in the code, but the
timeout error and POST body dump still occurred. I have the same feeling
you do: I'm going to have to rewrite this code using TCPClient. I did a
google group search for WebException Timeout and GetResponse() and got 69
hits. Over five of the responses from Microsoft's team stated issues with
bugs in .Net and their WebResponse class. Most of the bugs being different
issues too. .Net is showing it's youth. I'll start recoding it, but if you
or anyone else has any ideas, I'd love to hear 'em!

Thanks,
Nathan

"Trebek" <tre...@nospam.com> wrote in message
news:Itrcb.32008$uJ2....@fe3.columbus.rr.com...

Rich Blum

unread,
Sep 25, 2003, 6:29:50 PM9/25/03
to
"Nathan" <goo...@waitefamily.com> wrote in message news:<vn48ots...@corp.supernews.com>...
> This is a copy of a message at microsoft.public.dotnet.framework.clr:
>
> PROBLEM DESCRIPTION:
>
> The C# code should create an HTTP Header and POST with msg at Line 8 as
> the content and send it to port 22225 on the same machine. What happens
> is at Line 16 the HttpWebRequest creates the socket and sends ONLY the
> HTTP Header and HTTP Delimiter ( "\r\n\r\n" ). The Java server gets the
> Header and the Header's delimiter. Then, the C# program proceeds to
> line 19 and hangs. The Java server blocks on it's socket during the
> hang. C# hangs until the socket timeout is reached and then triggers
> the following message:
>
Nathan -

Are you sure your Java code follows the specs for a valid HTTP 1.1
server? From RFC 2616:

"Requirements for HTTP/1.1 origin servers:

- Upon receiving a request which includes an Expect
request-header
field with the "100-continue" expectation, an origin server
MUST
either respond with 100 (Continue) status and continue to read
from the input stream, or respond with a final status code.
The
origin server MUST NOT wait for the request body before
sending
the 100 (Continue) response. If it responds with a final
status
code, it MAY close the transport connection or it MAY continue
to read and discard the rest of the request. It MUST NOT
perform the requested method if it returns a final status
code."

Under HTTP 1.1 (which is what the WebRequest is using), the data
does not have to be contained in the same HTTP message as the POST
command. The server must recognize that and send a "100 Continue"
status response. Obviously your server did not do that, thus the
logjam. Hope this helps shed some light on your problem. Good luck
with your HTTP server and client.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471433012.html

Nathan

unread,
Sep 25, 2003, 7:43:41 PM9/25/03
to
Rich, You're Da Man!

You just sold me on your book ;-) The Java server was the problem, as I was
implementing it using RFC 2068 and not the newer RFC 2616 which .NET
obviously uses. Hence, GetResponse was waiting for my Java server to send
it a "100 (Continue)" response before it would sent me the body. After
sending me the continue, the body proceeded just fine.

Thanks for your post!

Nathan

"Rich Blum" <rich...@juno.com> wrote in message
news:ccdac0da.03092...@posting.google.com...

Chris Tanger

unread,
Oct 10, 2009, 1:57:27 PM10/10/09
to

For anyone else experiencing this issue it can also occur if you set HttpWebRequest.ServicePoint.BindIPEndPointDelegate and in the callback delegate you return an IPEndPoint whose IP address does not exist on any of the adapters on the local machine.

For example say our your machine has the IP addresses:
11.11.11.2
11.11.11.3
11.11.11.4

Say you bind your service point request to 11.11.11.5

This will cause the GetResponse method to hang and use 100% of your CPU (If you had 4 cores/CPUs it might only use 100% of one of them so 25%)

MakeRequest()
{
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(requestUri);
webReq.ServicePoint.BindIPEndPointDelegate = this._bindIPEndPointDel;
webReq.Method = "GET";

// Hangs here on this line
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
}

static BindIPEndPoint _bindIPEndPointDel = new BindIPEndPoint(BindIPEndPoint);
static IPEndPoint BindIPEndPoint(
ServicePoint servicePoint,
IPEndPoint remoteEndPoint,
int retryCount)
{
return new IPEndPoint(IPAddress.Parse("11.11.11.5"),0);
}

From http://www.developmentnow.com/g/36_2003_9_0_0_197194/HttpWebResponses-GetResponse-hangs-and-timeouts.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/

0 new messages