C# – HttpWebResponse.GetResponse() fiddler says “Response Header parsing failed. “

asp.netchttphttpwebrequesthttpwebresponse

I'm making a request to a website via the HttpWebRequest/HttpWebResponse objects.

I'm making several successful calls to the web site and every other call to the same dynamic page is failing.

In the debugger I'm getting a "Internal server error 500" fiddler also shows a 500 response and contains:

[Fiddler] Response Header parsing failed.
This can be caused by an illegal HTTP response earlier on this reused server socket--     for instance, a HTTP/304 response which illegally contains a body.
Response Data:
<plaintext> 
0D 0A 3C 21 44 4F 43 54 59 50 45 20 48 54 4D 4C 20 50 55 42 4C 49 43 20  ..<!DOCTYPE      HTML PUBLIC 
22 2D 2F 2F 57 33 43 2F 2F 44 54 44 20 48 54 4D 4C 20 34 2E 30 20 54 72  "-//W3C//DTD HTML 4.0 Tr
61 6E 73 69 74 69 6F 6E 61 6C 2F 2F 45 4E 22 3E 0D 0A 3C 48 54 4D 4C 3E  ansitional//EN">..<HTML>
0D 0A 09 3C 48 45 41 44 3E 0D 0A 09 09 3C 74 69 74 6C 65 3E 56 69 65 77  ...<HEAD>....<title>View

I've removed all the hex and viewed the page and is what I expect to be returned but for some reason the server is reporting a 500 and the HttpWebRequest object throws an exception on this.

I've tried all the other "fixes" for this issue and none work. It might just be malformed data sent from the server but is there a lower level object to use than HttpWebRequest that's not a pita to work with?

EDIT: I didn't include the entire hex/entire html block in the above example.
EDIT: Turning off fiddler I get this in the debugger

EDIT: So, from what I've seen the HttpWebResponse object is acting accordingly. The server is just flaky and sometime returns the same exact data with different http status codes. For a quick fix I just wrapped each call in a try/catch and in the catch block just retying the exact same call. So far it works great and semi-proves that it's the sites fault and not the HttpWebResponse object.

The server committed a protocol violation. Section=ResponseStatusLine

Best Answer

An HTTP 304 response means that the page content has not changed since the last time you hit the page (they're likely using caching). Hit the page less often or cache the response for use when this is encountered.

EDIT

The server is sending an invalid 304 response which contains data. This violates the HTTP spec and the HttpWebResponse/Fiddler is validly transforming it into a 500 as such.

EDIT

You may be able to keep using the HttpWebRequest/HttpWebResponse if you use the following setting in your app.config:

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