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

≡放荡痞女 提交于 2019-12-18 13:52:57

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/16759349/the-server-committed-a-protocol-violation-section-responseheader-detail-cr-must

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!