469,913 Members | 1,947 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

HttpWebResponse and ServerProtocolViolation

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());
}

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Apr 12 '06 #1
1 5626
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,
--
Joerg Jooss
ne********@joergjooss.de
Apr 13 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by hlabbott | last post: by
1 post views Thread by Jason Manfield | last post: by
3 posts views Thread by Nuno Magalhaes | last post: by
1 post views Thread by Brent | last post: by
2 posts views Thread by Nuno Magalhaes | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.