Andy @ Work

Tuesday, January 24, 2006

Thank God for Reflector

One of the projects I'm working on requires integration with an online credit card gateway. The interface is very simple, of the single page to post parameters at variety.

So I implemented by solution using good old faithful System.Net.WebClient. Imagine my surprise when UploadValues was raising the following exception:

The server committed a protocol violation.
Section=ResponseHeader Detail=CR must be followed by LF


I had a quick look at the raw response, and sure enough the response header and body were only separated by a single CR instead of CRLF (why an IIS 6 server is responding in this fashion I have no idea).

Since I have no control over the external gateway (even though technically they are sending malformed responses) I thought that I could implement my simple UploadValues() method using the Socket class.

That I idea seemed good for about 5 seconds when I recalled that I'm calling a web page over SSL, making the work of implementing UploadValues a lot more difficult.

As the title of this post suggests, I fired up Reflector to see the code that was raising this exception to determine if there was a work around. 30 minutes later I found one (after I disassembled the entire System.dll to a temporary folder). System.Net.WebHeaderCollection has two methods for parsing response headers:


ParseHeaders()
ParseHeadersStrict()


ParseHeadersStrict was raising the exception. But plain old ParseHeaders looked to be a little more forgiving.

So I used Reflectors analyzer to determine who was calling ParseHeadersStrict and discovered the System.Net.Connection.ParseResponseData() method. ParseResponseData makes a decision to call either ParseHeaders or ParseHeadersStrict based on the value of SettingsSectionInternal.Settings.UseUnsafeHeaderParsing (found in System.Net.Configuration).

By this point I can smell victory. Using VS.Net 2005's excellent intellisense for configuration files, I added the following:


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


Without Reflector I don't know if I would have ever found the solution so quickly.

1 Comments:

  • Hey, I dont know what reflector is, so I'll thank god for your blog instead...
    My web page uses the System.Net.HttpWebRequest.GetResponse to read remote web pages.
    I've never seen this error "The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF" come up before, until today. Now it appears that two web pages I check are exhibiting this behavior - http://community.divx.com and http://www.morpheus.com/

    By Anonymous Lance, at 4:49 AM  

Post a Comment

<< Home