Tech Off Thread

2 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Help! HttpWebResponse errors

Back to Forum: Tech Off
  • User profile image
    sysrpl

    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?

  • User profile image
    katokay

    Place this xml in the application configuration file.

    <system.net>
      <settings>
       <httpWebRequest useUnsafeHeaderParsing="true"/>
      </settings>
     </system.net>

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.