服务器提交了违反协议的协议.在WinForms中,Section = ResponseHeader Detail = CR后面必须是LF? [英] The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF, In WinForms?

查看:176
本文介绍了服务器提交了违反协议的协议.在WinForms中,Section = ResponseHeader Detail = CR后面必须是LF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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);

服务器是我的页面,处于路由器的配置中.它可以从浏览器正常工作,但是使用我的代码下载时,它会在消息

中抛出WebException

服务器犯了协议冲突.在WinForms中,节= ResponseHeader Detail = CR后必须是LF.

我发现一个解决方案,如果他们使用的是ASP.net,可以在web.config中添加以下内容:

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

但是,我正在制作WinForms应用程序,因此这对我不起作用.有什么替代方法可以解决此问题?

解决方案

首先,添加app.config文件与添加任何其他文件一样容易,此处.

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?

解决方案

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.

这篇关于服务器提交了违反协议的协议.在WinForms中,Section = ResponseHeader Detail = CR后面必须是LF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆