Marvin Marvin - 3 months ago 76
ASP.NET (C#) Question

The server committed a protocol violation. Section=ResponseStatusLine

Please, someone help me. I've been researching this for hours, have tried lots of different suggested fixes, and I'm at a total loss as to why I am still getting this error.

I have an ASP.NET 4.0 web page with the following code in it:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetHref);
// request.KeepAlive = false;
// request.ProtocolVersion = System.Net.HttpVersion.Version10;
// request.ServicePoint.Expect100Continue = false;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();


I've tried every combination I can imagine of un-commenting the above 3 commented lines.

I have this in my web.config:

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


When I am running Fiddler on my dev machine, the page runs without exception and fiddler shows this message at the top of the return:


HTTP/1.0 200 This buggy server did not return headers


Without Fiddler running, I get the error every time on the
request.GetResponse()
line.

Answer

Well, I tried every suggestion I could find here on StackOverflow and elsewhere on the web, but never got an implementation of HttpWebRequest to work for me, so I tried using TcpClient instead after reading this: How To Set useUnsafeHeaderParsing For .NET Compact Framework

To get it to work for me, I had to modify this line:

 sw.WriteLine(string.Format("GET /{0} HTTP/1.1", pathAndQueryString));

to:

 sw.WriteLine(string.Format("GET /{0} {1}", pathAndQueryString, Environment.NewLine+Environment.NewLine));

And that worked.