N
Nathan
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
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