Hi, I am having a problem with HttpWebRequest exceptions being
thrown when it receives what it sees as invalid response
headers. The exception is throw when calling
request.GetResponse().
I need to relax the way HttpWebRequest handles headers, in
order to get the response text and parse it. The code I 'm using
is something like below. The difference is the real code uses a
valid url and ten kilobytes 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 the problem is that HttpWebRequest doesn't like the
returned header. This what the returned header looks like. I got
these headers from Firefox/IE, so I figure I should also be able to
get them from an HttpWebRequest.
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 of 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>
How can I get HttpWebRequest to relax and not throw
exceptions? I really need to receive the response text for my
application. Also, when try catching the WebException and
check the Response property, it's null.
Can someone please help me here?
-
-
Place this xml in the application configuration file.
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true"/>
</settings>
</system.net>
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.