473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 5845
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Denny Rue | last post by:
I’ve created VB code to download files from a web site through the use of HTTPWebRequest, HTTPWebResponse and BinaryReader. The HTTPWebRequest has a TimeOut property to limit how long it waits...
0
by: hlabbott | last post by:
Hi I'm having a problem displaying attachments correctly. I have messages with attachments stored on Exchange2000 and want to be able to click a hyperlink in my project like in OWA and see an...
1
by: Ravi | last post by:
Hi , i am trying to pass the same session Id to all the webrequest, but sometimes the response.cookies returns zero and sometimes one. is this to do something with cookies expire. In this sample...
13
by: Jason Manfield | last post by:
For some URLs (e.g.http://v3.espacenet.com/origdoc?DB=EPODOC&IDX=WO2005028634&F=0&QPN=WO2005028634), the content length for the HttpWebResponse I get with request.GetResponse in empty. The...
1
by: Jason Manfield | last post by:
Why does HttpWebResponse.CharacterSet always return ISO-8859-1? I am accessing a Chinese Web site (http://cn.yahoo.com) -- View Source in IE shows the character set to be "gb2312", but the...
3
by: Nuno Magalhaes | last post by:
Hello, In a simple thread I have a code like the one below: public void ProtectionRun() { while(true) { //Sleep thread for one minute //Thread.Sleep(60000); HttpWebRequest
2
by: Noggin The Nog | last post by:
Hi all, I've been trying to get HttpWebResponse to work, but whenever I try I get "The operation has timed-out". I'm simply trying to send an HTTP request querystring to a remote website and read...
1
by: Brent | last post by:
I thought I was doing a simple thing, here -- asking a server for a text document, getting the first 150 lines, and then returning the lines. But I keep getting timeout errors: "Exception...
2
by: Nuno Magalhaes | last post by:
In pages that there is no content length, how does HttpWebResponse knows where the page ends? And what kind of objects/methods does it retrieve? Does it only retrieve the initial page without any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.