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/