解决The server committed a protocol violation. Section=ResponseStatusLine 的方法

本文介绍了解决使用HttpWebResponse时出现的Theservercommittedaprotocolviolation错误的方法。该问题源于部分服务器返回的HTTP头部不符合规范,导致.NET框架无法解析。通过在配置文件中设置useUnsafeHeaderParsing为true,可以绕过此限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编写了一个程序,用于抓取网页上的一些信息,原来运行的很正常,某一天发生了错误

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值