我需要在我的路由器中读取一个位置,但我得到了以下异常-
ServerProtocolViolation "The server committed a protocol violation.
Section=ResponseHeader Detail=CR must be followed by LF"
当我使用.DownloadString(url)
函数时,就会出现这种情况。有没有办法让WebClient
忽略协议违规?谷歌搜索告诉我,我应该在某个地方设置useUnsafeHeaderParsing
选项。我可以通过程序来做吗?如果我使用它会有什么问题呢?
编辑:附加代码-
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());
}
}
发布于 2017-09-27 18:55:14
我在我自己的own服务器中遇到了这个问题,在标题中我更改了
HTTP/1.x 200 OK
至
HTTP/1.0 200 OK
现在,当我使用浏览器(chorome或...)时,它可以正常工作。或在WebClient (c#)中
https://stackoverflow.com/questions/3142403
复制相似问题