48

I am trying to use a WebClient / HttpWebRequest to download some data from a server. I use the following code to do so:

WebClient client = new WebClient();
client.Credentials = new NetworkCredential("admin", "password");
Stream datastream = client.OpenRead("http://routerlogin.com/cgi-bin/CF_logs.html");
StreamReader reader = new StreamReader(datastream);

The server is my page is in my router's configuration. It works fine from a browser, but when downloaded using my code it throws a WebException with the message

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

I have found a solution one would use if they were using ASP.net, adding the following to web.config:

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

However, I am making a WinForms app so this won't work for me. What alternatives are there to fix this problem?

Abhishek
  • 2,925
  • 4
  • 34
  • 59
msbg
  • 4,852
  • 11
  • 44
  • 73
  • 1
    this should work for all kinds of applications, not just asp.net – shriek May 26 '13 at 13:24
  • where should I add it? There is no web or app.config. – msbg May 26 '13 at 13:55
  • 2
    Strange thing i am experiencing is that that error coming on one pc withing our network but other pc with the same application working fine. When i add above mention entry that pc also starts working fine. Question is why that problem is just on a single pc (which do have a new installation) – Kamran Shahid Aug 10 '15 at 11:33
  • Just wanted to add my experience, which was the same as Kamran Shahid where actually all the other computers worked but mine did not. The only out of left field guess I have is the strange character I see in Fiddler when I check the response headers and in the middle of one of the set-cookie passed. Notepad++ renders this special char as SOH which I googled is start of header. [link]http://eureka.greenhead.com/server-committed-a-protocol-violation-cr-must-be-followed-by-lf/ after reading this, I'm not sure there is much else to do except but the server. config change above did not fix it. – spacebread Apr 24 '18 at 18:04

3 Answers3

29

First, adding an app.config file is just as easy as adding any other file, How to: Add an Application Configuration File to a C# Project

Then you just have to add that code snippet above to that new app.config.

Another way of setting that property via code, avoiding the need for an app.config is shown here or here.

shriek
  • 5,157
  • 2
  • 36
  • 42
2

Your problem might not require any app.config changes (and in my case, this configuration change made no difference). I would try modifying your Accept: header, as suggested in this link.

In my case, I create an HttpWebRequest directly, so my solution was to add the following:

request.Accept = "text/html, application/xhtml+xml, */*"
Todd Myhre
  • 1,310
  • 1
  • 10
  • 15
0

I had same issue. I fix it this way. header separate \r\n\r\n from data

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 08 '22 at 06:09