WebException thrown by HttpWebRequest.GetResponse() method

  • Thread starter Sergey Shcherbakov via .NET 247
  • Start date
S

Sergey Shcherbakov via .NET 247

Hi,

I have a problem using HttpWebRequest class.
I create an application, which communicates with a network device(not a PC), which has a built in HTTP 1.1 server listening onport 80. For some requests, which I send to the server itanswers with a short byte sequence:

HTTP/1.1 200 OK\r\n

and that's it, no more data (I check network traffic usingEthereal network monitor).
In case of such response from the server, I get a WebException ona call to HttWebRequest.GetResponse() method saying: "Theunderlying connection was closed: An unexpected error occured ona receive". If there are some data coming from server, thenGetResponse() method returns instance of HttpWebResponseproperly.

here is a code, where I get a WebException:

HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(TargetUriString);
hwr.Timeout = 10000;
hwr.Method = "POST";
hwr.UserAgent = "AddressEditor";
hwr.ContentLength = buf.Length;
hwr.KeepAlive = true;
HttpWebResponse hwResponse = null;
Stream sr = null;
try
{
// Write data
Stream s = hwr.GetRequestStream();
s.Write(buf,0,buf.Length);
s.Close();
// Read response
hwResponse = (HttpWebResponse) hwr.GetResponse(); //WebException is thrown here !!!
if(hwResponse.StatusCode == HttpStatusCode.OK)
{
if(hwr.HaveResponse)
{
sr = hwResponse.GetResponseStream();
int offset=0;
int remaining = buf.Length;
while (remaining > 0)
{
int read = sr.Read(buf, offset, remaining);
if (read <= 0)
break;
remaining -= read;
offset += read;
}
}
}
}
catch(WebException we)
{
Console.WriteLine(we.Message);
}
finally
{
if(sr != null)
sr.Close();
if(hwResponse != null)
hwResponse.Close();
}

Actually, I am not completely sure, whether the problem is im mycode, .NET framework or device HTTP server, because exceptionoccurs immediately after a call to GetResponse() and the deviceusually answers with "HTTP/1.1 200 OK\r\n" after a approximately5 seconds timeout, because of processing of input data. So theGetResponse() does not wait for response and just throwsexception. It does not happen with this code when the serverreturns some data.
Could you please give me a hint:
1) Am I correctly using HttpWebrequest to write data to theremote server?
2) How to make my code block and wait untill "HTTP/1.1 200OK\r\n" response comes from the server? (The server becomesunavailable untill it processes all input data sent by me and Ican not send next requests to it untill this one ends with 200OK)

Thank you for any help, it is really apreciated.
 
J

Joerg Jooss

Sergey said:
Hi,

I have a problem using HttpWebRequest class.
I create an application, which communicates with a network device
(not a PC), which has a built in HTTP 1.1 server listening on port
80. For some requests, which I send to the server it answers with a
short byte sequence:

HTTP/1.1 200 OK\r\n

and that's it, no more data (I check network traffic using Ethereal
network monitor). In case of such response from the server, I get a
WebException on a call to HttWebRequest.GetResponse() method saying:
"The underlying connection was closed: An unexpected error occured on
a receive". If there are some data coming from server, then
GetResponse() method returns instance of HttpWebResponse properly.
[...]
Actually, I am not completely sure, whether the problem is im my
code, .NET framework or device HTTP server, because exception occurs
immediately after a call to GetResponse() and the device usually
answers with "HTTP/1.1 200 OK\r\n" after a approximately 5 seconds
timeout, because of processing of input data. So the GetResponse()
does not wait for response and just throws exception. It does not
happen with this code when the server returns some data. Could you
please give me a hint: 1) Am I correctly using HttpWebrequest to
write data to the remote server? 2) How to make my code block and
wait untill "HTTP/1.1 200 OK\r\n" response comes from the server?
(The server becomes unavailable untill it processes all input data
sent by me and I can not send next requests to it untill this one
ends with 200 OK)

Your code is fine. This is a server error. The HTTP response shown
above is a protocol violation.

A HTTP 1.1 response must either contain
a) a valid Content-Length header or
b) a Connection: close header or
c) a Transfer-Encoding: chunked header (and a chunked response of
course)

You can try to work around this bug by setting HttpWebRequest.KeepAlive
to false or instead use HTTP 1.0 as protocol version.

Cheers,
 
H

Haacked

There's a small chance you're running into the Expect-100
problem. I wrote about it here:
http://haacked.com/archive/2004/05/15/449.aspx

The solution in my case was to set the static property
System.Net.ServicePointManager.Expect100Continue
to false.

Good luck.
-----Original Message-----
Hi,

I have a problem using HttpWebRequest class.
I create an application, which communicates with a
network device (not a PC), which has a built in HTTP 1.1
server listening on port 80. For some requests, which I
send to the server it answers with a short byte sequence:
HTTP/1.1 200 OK\r\n

and that's it, no more data (I check network traffic
using Ethereal network monitor).
In case of such response from the server, I get a
WebException on a call to HttWebRequest.GetResponse()
method saying: "The underlying connection was closed: An
unexpected error occured on a receive". If there are some
data coming from server, then GetResponse() method
returns instance of HttpWebResponse properly.
here is a code, where I get a WebException:

HttpWebRequest hwr = (HttpWebRequest) WebRequest.Create (TargetUriString);
hwr.Timeout = 10000;
hwr.Method = "POST";
hwr.UserAgent = "AddressEditor";
hwr.ContentLength = buf.Length;
hwr.KeepAlive = true;
HttpWebResponse hwResponse = null;
Stream sr = null;
try
{
// Write data
Stream s = hwr.GetRequestStream();
s.Write(buf,0,buf.Length);
s.Close();
// Read response
hwResponse = (HttpWebResponse) hwr.GetResponse
(); // WebException is thrown here !!!
if(hwResponse.StatusCode == HttpStatusCode.OK)
{
if(hwr.HaveResponse)
{
sr = hwResponse.GetResponseStream ();
int offset=0;
int remaining = buf.Length;
while (remaining > 0)
{
int read = sr.Read(buf, offset, remaining);
if (read <= 0)
break;
remaining -= read;
offset += read;
}
}
}
}
catch(WebException we)
{
Console.WriteLine(we.Message);
}
finally
{
if(sr != null)
sr.Close();
if(hwResponse != null)
hwResponse.Close();
}

Actually, I am not completely sure, whether the problem
is im my code, .NET framework or device HTTP server,
because exception occurs immediately after a call to
GetResponse() and the device usually answers
with "HTTP/1.1 200 OK\r\n" after a approximately 5
seconds timeout, because of processing of input data. So
the GetResponse() does not wait for response and just
throws exception. It does not happen with this code when
the server returns some data.
Could you please give me a hint:
1) Am I correctly using HttpWebrequest to write data to the remote server?
2) How to make my code block and wait untill "HTTP/1.1
200 OK\r\n" response comes from the server? (The server
becomes unavailable untill it processes all input data
sent by me and I can not send next requests to it untill
this one ends with 200 OK)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top