编写了一个程序,用于抓取网页上的一些信息,原来运行的很正常,某一天发生了错误
HttpWebResponse.GetResponse() 抛出以下异常:
The server committed a protocol violation. Section=ResponseStatusLine
在网上查了好久,发现很多人遇到了这个问题
这个是因为微软没有容忍不符合RFC 822中的httpHeader必须以CRLF结束的规定的服务器响应。
Apparently the problem is that starting with .NET 1.1 SP2 the HTTP protocol usage has been made more strict and any issApparently the problem is that starting with .NET 1.1 SP2 the HTTP protocol usage has been made more strict and any issue ue in the HTTP headers - such as returning only a CR intead of CRLF pair at the end of a header - apparently throws the header for a loop.
通过修改配置文件解决:
For windows form applcation, create a new configuration file [application name].exe.config in the same directory of execution file. For web application, please modify web.config like below:
< ?xml version="1.0" encoding="utf-8" ?>
< configuration>
< system.net>
< settings>
< httpWebRequest useUnsafeHeaderParsing= "true " />
< /settings>
< /system.net>
< /configuration>
本文介绍了解决使用HttpWebResponse时出现的Theservercommittedaprotocolviolation错误的方法。该问题源于部分服务器返回的HTTP头部不符合规范,导致.NET框架无法解析。通过在配置文件中设置useUnsafeHeaderParsing为true,可以绕过此限制。
943

被折叠的 条评论
为什么被折叠?



