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.
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.