C# 服务器违反了协议。Section=ResponseHeader Detail=CR 后面必须跟 LF,在 WinForms 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16759349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF, In WinForms?
提问by msbg
I am trying to use a WebClient / HttpWebRequest to download some data from a server. I use the following code to do so:
我正在尝试使用 WebClient / HttpWebRequest 从服务器下载一些数据。我使用以下代码来做到这一点:
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 WebExceptionwith the message
服务器是我的页面在我的路由器配置中。它在浏览器中运行良好,但是当使用我的代码下载时,它会抛出WebException带有消息的
The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF, In WinForms?.
服务器违反了协议。Section=ResponseHeader Detail=CR 后面必须跟 LF, In WinForms?。
I have found a solution one would use if they were using ASP.net, adding the following to web.config:
我找到了一个如果他们使用 ASP.net 会使用的解决方案,将以下内容添加到 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?
但是,我正在制作一个 WinForms 应用程序,所以这对我不起作用。有什么替代方法可以解决这个问题?
采纳答案by shriek
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
首先,添加 app.config 文件与添加任何其他文件一样简单,如何:将应用程序配置文件添加到 C# 项目
Then you just have to add that code snippet above to that new app.config.
然后你只需要将上面的代码片段添加到新的 app.config 中。
Another way of setting that property via code, avoiding the need for an app.config is shown hereor here.
回答by Todd Myhre
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.
您的问题可能不需要任何 app.config 更改(就我而言,此配置更改没有任何区别)。我会尝试修改您的 Accept: 标头,如此链接中所建议。
In my case, I create an HttpWebRequest directly, so my solution was to add the following:
就我而言,我直接创建了一个 HttpWebRequest,所以我的解决方案是添加以下内容:
request.Accept = "text/html, application/xhtml+xml, */*"

