Help! HttpWebRequest throwing exceptions

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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.


.



Relevant Pages

  • RE: HTTPS WebRequest and WebResponse
    ... > // Creates an HttpWebRequest for the specified URL. ... > // Displays all the headers present in the response received from the URI. ... > // Obtain a 'Stream' object associated with the response object. ... > StreamReader readStream = new StreamReader(ReceiveStream, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Capturing XML pages from Server
    ... Thanks for your response and glad that it's of assistance. ... Microsoft Online Support ... WE can use the HttpWebRequest to send get/post ... |> Get Secure! ...
    (microsoft.public.dotnet.framework.aspnet)
  • problem WebRequest and WebResponse
    ... I have a problem using HttpWebRequest so I wish somebody can help me. ... HttpWebRequest request = ... WebResponse response = null; ... connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& ...
    (microsoft.public.dotnet.languages.csharp)
  • HTTPS WebRequest and WebResponse
    ... // Creates an HttpWebRequest for the specified URL. ... // Displays all the headers present in the response received from the URI. ... // Obtain a 'Stream' object associated with the response object. ... StreamReader readStream = new StreamReader(ReceiveStream, ...
    (microsoft.public.dotnet.languages.csharp)
  • problem with HttpWebRequest GetResponse
    ... "POST"s to an internal webserver via HttpWebRequest. ... going over and coming back I can see that valid data is being retrieved from ... So how can I ignore the exception and just get to the response ...
    (microsoft.public.dotnet.framework.aspnet)