C#: Handling WebClient “protocol violation”

前端 未结 2 1792
旧时难觅i
旧时难觅i 2020-12-03 17:14

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

ServerProtocolViolation \"The server committed a protocol violation. 
                    


        
相关标签:
2条回答
  • 2020-12-03 17:37

    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#)

    0 讨论(0)
  • 2020-12-03 18:03

    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.

    0 讨论(0)
提交回复
热议问题