今天在HttpWebRequest发送一个网页请求的时候,HttpWebResponse返回了一个奇怪的错误信息:

这个Http协议请求类可是微软封装的,我使用的流程可是中规中矩,不可能是我写错代码,然而看了下抓包工具抓的包,返回一切正常,所以只有一种可能就是对方服务器返回的标头格式不符合微软的解析规则。 因此脑袋里第一个想到的就是用Socket重写HttpWebResponse,可是想了下,HttpWebResponse本身封装的已经不错了,如果再去重写还不一定会比微软写的好,况且因为这一个小小的问题就重新去造一个非常复杂精细的轮子,旷日持久不说,水平和质量也令人怀疑。于是乎上网找了下对策。

网上大部分都是在app.Config配置里设置useUnsafeHeaderParsing:

<?xml version="1.0"?>
<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
</configuration>

这个方法证明可行,但是想了下,很多朋友都很不喜欢一个小小的程序因为这个事带上个配置文件,总感觉心里毛毛的。要是在程序里解决多好。于是乎又花了点时间,在一个国外的论坛里找到了解决方案,用了反射,直接操作System.Net.Configuration.SettingsSectionInternal类下的私有字段。虽然反射会带来性能上的影响,但是这里貌似没有更好的办法,因为不能操作一个封装好的私有变量。

 public static bool SetAllowUnsafeHeaderParsing20(bool useUnsafe)
{
//Get the assembly that contains the internal class
System.Reflection.Assembly aNetAssembly = System.Reflection.Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
if (aNetAssembly != null)
{
//Use the assembly in order to get the internal type for the internal class
Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
if (aSettingsType != null)
{
//Use the internal static property to get an instance of the internal settings class.
//If the static instance isn't created allready the property will create it for us.
object anInstance = aSettingsType.InvokeMember("Section",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.NonPublic, null, null, new object[] { }); if (anInstance != null)
{
//Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
System.Reflection.FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (aUseUnsafeHeaderParsing != null)
{
aUseUnsafeHeaderParsing.SetValue(anInstance, useUnsafe);
return true;
}
}
}
}
return false;
}

这个方法一定要在HttpWebRequest开始响应之前设置,否则会没有任何效果。

服务器提交了协议冲突. Section=ResponseHeader Detail=CR...的解决方案总结的更多相关文章

  1. HttpWebRequest出错 服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 LF

    服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 LF  The server committed a protocol violation. Se ...

  2. 关于 服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 LF 错误

    用WebClient 去下载数据时发现有服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 LF错误,解决办法 1.在app.config种添加 we ...

  3. WebRequest请求错误(服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 LF)

    WebRequest请求错误(服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 LF)解决办法,天津config文件,增加一个配置如下 <?x ...

  4. c# winform 服务器提交了协议冲突. Section=ResponseStatusLine

    [转] 最近在用.net写一个网络蜘蛛,发现对有的网站用HttpWebrequest抓取网页的时候会报错,捕获异常提示:"服务器提交了协议冲突 Section=ResponseStatusL ...

  5. httpwebrequest 服务器提交了协议冲突. section=responsestatusline

    调用接口的时候,包: httpwebrequest 服务器提交了协议冲突. section=responsestatusline 解决方案: req.KeepAlive = false; req.Al ...

  6. 解决WebService/WCF调用时报错"服务器提交了协议冲突. Section=ResponseStatusLine"问题

    今天更新了一个网站,新增了一个页面,调用WebService,在测试环境好好的,部署到正式环境后就莫名报错: 服务器提交了协议冲突. Section=ResponseStatusLine 网上查了好多 ...

  7. 关于“服务器提交了协议冲突. Section=ResponseStatusLine"问题

    你的问题的原因是这样的,ASP.Net 2.0 增强了安全性,对一些有危害的http 头进行了判断,比如url中有空格的情况,以帮助网站提高网络攻击的防御能力.如果你的http头中有一些ASP.NET ...

  8. C#调取接口时报错:服务器提交了协议冲突. Section=ResponseStatusLine

    private Dictionary<string, Object> GetLocation(string imei) { #region===代码=== string serviceAd ...

  9. 服务器提交了协议冲突。Section=ResponseHeader Detail=标头名称无效

    服务器提交了协议冲突.Section=ResponseHeader Detail=CR 后面必须是LF. 微软没有容忍不符合RFC 822中的httpHeader必须以CRLF结束的规定的服务器响应所 ...

随机推荐

  1. javascript 中利用正则匹配 时间

    本文从百度知道回来中粘贴过来,当做一个笔记,因为,说的很详细 最简单的正则 如 : \d{4}-\d{2}-\d{2}但是实际情况却不是那么简单,,要考虑,有效性和闰年等问题..... 对于日期的有效 ...

  2. 配置文件报错:元素类型 "XXX" 必须后跟属性规范 ">" 或 "/>"

    这是一个比较常见的配置错误,一般分两步. 有的情况的确是xml文件出现了语法问题,可以让IDE来帮我们检查xml文件是否真的出现了语法错误.把xml内容复制进IDE中,让IDE帮我们检查错误. 如上图 ...

  3. 【HDU 5730】Shell Necklace

    http://acm.hdu.edu.cn/showproblem.php?pid=5730 分治FFT模板. DP:\(f(i)=\sum\limits_{j=0}^{i-1}f(j)\times ...

  4. 【数据结构(高效)/暴力】Parencodings

    [poj1068] Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26686   Accepted ...

  5. MySort

    实验概述: 本次试验的内容:模拟实现Linux下Sort -t : -k 2的功能.参考Sort的实现.提交码云链接和代码运行截图. 截图如下 实验过程 在实验课上真的很崩溃,可以说脑子里一团乱麻,下 ...

  6. nginx配置本地https

    客户端如何验证服务器的证书呢?服务器自己的证书必须经过某"权威"证书的签名,而这个"权威"证书又可能经过更权威的证书签名,这么一级一级追溯上去,最顶层那个最权威 ...

  7. Problem B: 颠倒字符串

    #include<stdio.h> #include<string.h> //用来调用strlen(str)函数 int main() { int i,n; ]; while( ...

  8. for循环、for-in、forEach、for-of四大循环

    平时自己在写一些小栗子的时候,用到的基本上是for循环,因为在学专业课(C,C++,JAVA,...)的时候用的最多的就是for循环,不过for循环的效率也是比较高的. 但是for循环在写的时候,涉及 ...

  9. NHibernate官方文档中文版——批量插入(Batch inserts)

    A naive approach t7o inserting 100 000 rows in the database using NHibernate might look like this: 一 ...

  10. jQuery:validate内置验证的使用

    (1)required:true 必输字段(2)remote:"check.php" 使用ajax方法调用check.php验证输入值(3)email:true 必须输入正确格式的 ...