17

I need to read a location in my Router, but I get the following exception -

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

This occurs when I use the .DownloadString(url) function. Is there any way to make WebClient ignore protocol violation? Searches in Google tell me I should set the useUnsafeHeaderParsing option somewhere. Can I do it through program? What's the catch if I use it?

Edit: Attaching code -

    public Readlog() {
        WebClient wc = new WebClient();

        string url = @"http://192.168.0.1/setup.cgi?next_file=log.htm&todo=cfg_init";
        Console.WriteLine(url);
        try {
            //wc.Headers.Add("User-Agent", "Mozilla/5.0(Windows; U; Windows NT 5.2; rv:1.9.2) Gecko/20100101 Firefox/3.6");
            wc.Credentials = new NetworkCredential("admin", "admin");
            //Next line causes exception System.Net.WebException
            //Message - "The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF"
            //May be I need to use useUnsafeHeaderParsing somehow to avoid that
            string result = wc.DownloadString(url);
            Console.WriteLine(result);
        } catch (WebException we) {
            System.Diagnostics.Trace.WriteLine(we.ToString());
        }
    }
Noctis
  • 11,507
  • 3
  • 43
  • 82
KalEl
  • 8,978
  • 13
  • 47
  • 56

2 Answers2

26

Looks like the easiest way is including a .config file with your app containing the following:

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

However it's also possible to do it within the code but it seems a little messy:

http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/ff098248-551c-4da9-8ba5-358a9f8ccc57

Also note that the MSDN definition of that property is

Setting this property ignores validation errors that occur during HTTP parsing.

http://msdn.microsoft.com/en-us/library/system.net.configuration.httpwebrequestelement.useunsafeheaderparsing.aspx

So I'd say it's fairly safe to use, although it does mention to only use it for backwards compatibility.

Lummo
  • 1,159
  • 9
  • 14
  • I feel like you can do this another way. My gut says service point manager or subglassing HttpWebRequest – Christopher Tarquini Jun 30 '10 at 05:22
  • 7
    Thanks, it works although it is messy as you said. This should have been as simple as setting a property in the WebClient class. The decision of Microsoft to (almost) hard code compliance with RFC is highly questionable - it should have been left open to the programmar. This almost looks like a shortsight - in thinking that the programmer always has access to fix server side protocol violations. – KalEl Jun 30 '10 at 13:02
-1

I had this problem in my own webserver, in header I changed

HTTP/1.x 200 OK

to

HTTP/1.0 200 OK

now it works when I use a browser (chorome or ...) or in WebClient (c#)

mrbm
  • 1,136
  • 1
  • 12
  • 36