HttpWebResponse and ServerProtocolViolation

J

Joe

I'm creating an application that HTTP posts some form data, and is
supposed to read the response. When I actually do this form posting from
a web page, I get the response exactly as I would expect. However, when I
try to do this form posting programmatically, I get a WebException where
the Status is set to ServerProtocolViolation. As far as I can tell, this
status tells me that the response is not a standard HTTP response. Ok, I
know this already, but I still want that response regardless if it's a
standard HTTP response or not. (Remember, in a browser form post, this
all works as expected. IE, Opera, FireFox all process the post just
fine...)

Here's the code I'm using to do the HTTP post. What do I need to do to
post this data, and get the response back regardless if it's a standard
Http response or not? Perhaps use something other than anHttpWebResponse
object? I just want to get the response regardless of what it is...

// used on each read operation
byte[] buf = new byte[8192];

// prepare the web page we will be asking for
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("http://192.168.100.113:666");
HttpWebResponse response = null;

byte[] bytes = System.Text.Encoding.ASCII.GetBytes("xmlValue=1");
request.Accept = "*/*";
request.ContentLength = bytes.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
System.IO.Stream os = request.GetRequestStream();
os = request.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Flush();
os.Close();

// execute the request
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException err)
{
// here's where I get the exception, the Status property is set
// to ServerProtocolViolation
MessageBox.Show(err.Status.ToString());
}
 
J

Joerg Jooss

Thus wrote Joe,
I'm creating an application that HTTP posts some form data, and is
supposed to read the response. When I actually do this form posting
from a web page, I get the response exactly as I would expect.
However, when I try to do this form posting programmatically, I get a
WebException where the Status is set to ServerProtocolViolation. As
far as I can tell, this status tells me that the response is not a
standard HTTP response. Ok, I know this already, but I still want
that response regardless if it's a standard HTTP response or not.
(Remember, in a browser form post, this all works as expected. IE,
Opera, FireFox all process the post just fine...)

Remember that these HTTP implementations likely all evolved as HTTP did,
and thus needed to support certain quirks and non-standard behaviors -- pretty
much like HTML parsers. Unfortunately, what was probably considered a quirk
in 2000 may have been discoverd to be a serious security hole in 2006.
Here's the code I'm using to do the HTTP post. What do I need to do
to post this data, and get the response back regardless if it's a
standard Http response or not? Perhaps use something other than
anHttpWebResponse object? I just want to get the response regardless
of what it is...

// used on each read operation
byte[] buf = new byte[8192];
// prepare the web page we will be asking for
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("http://192.168.100.113:666");
HttpWebResponse response = null;
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("xmlValue=1");
request.Accept = "*/*";
request.ContentLength = bytes.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
System.IO.Stream os = request.GetRequestStream();
os = request.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Flush();
os.Close();
// execute the request
try
{
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException err)
{
// here's where I get the exception, the Status property is set
// to ServerProtocolViolation
MessageBox.Show(err.Status.ToString());
}

Save for my usual moaning about exception handling and using blocks, the
only evil in your code seems to be posting to port 666 -- SCNR ;-)

You can try and get away by flicking the unsafeHeaderParsing switch (see
http://tinyurl.com/nctdf). If that doesn't work, you may want to look into
using another library, plain old socket programming, or fixing the server
side...

Cheers,
 

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