By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,013 Members | 1,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,013 IT Pros & Developers. It's quick & easy.

Help! HttpWebRequest throwing exceptions

P: n/a
Hi, I'm writing a console application and am having a problem with HttpWebRequest
when posting data to a webserver. A "System.Net.WebException: The server committed a
protocol violation" is always being thrown when getting the response from a specific
host. I'm pretty sure the framework's implementation doesn't like the formating or
status code of the returned response header. It throws the exception without giving
me a way of handling it (the WebException.Response propery is always null).

I need a way to relax how HttpWebRequest handles response headers, because I want to
get and parse the response text. The code I'm using is something like the snippet
below. In the actual code, I use a valid url and 10KB of data loaded from a textfile.

string url = "http://www.somedomain.com/some/action";
string data = "data=10k+of+encoded+data+loaded+here";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) {
writer.Write(data);
}
// The line below causes this error
// Error: System.Net.WebException: The server committed a protocol violation
// at System.Net.HttpWebRequest.CheckFinalStatus(Boolean mustThrow)
// at System.Net.HttpWebRequest.GetResponse()
// at Testing.Post()
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
data = reader.ReadToEnd();
}
request.Close();
return data;

You can see where the problem exception is raised. I used an alternate method to get
the actual response in it's entirety. It looked something like this:

HTTP/1.1 200 OK
Date: Mon, 24 Apr 2006 05:05:25 GMT
Server: LiveRack
Set-Cookie: name_cookie=someuser; expires=Tue, 24 Apr 2007 05:05:25 GMT;
path=/some/action; domain=.somedomain.com
Set-Cookie: <a few really big cookies here>
Set-Cookie: <appoximately 10 kilobytes worth>
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

<html><head><title>Please wait ...</title><script langage="Javascript">
<!-- some code here// -->
</script>
<body>
<p align="center">Comment added.</p>
</body>
</html>

One thing to note is that cookies are REALLY big, but they work in Firefox/IE, just
not programatically with HttpWebRequest. How can I get HttpWebRequest to relax how it
handles responses and not throw exceptions? I really need to receive the response
text for this console application.

Any help would be greatly appreciated.
Apr 24 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.