码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2016-12-09 00:03:01      阅读:441      评论:0      收藏:0      [点我收藏+]

标签:setting   提交   static   http   ant   bre   seh   unsafe   web   

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

The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF 

主体意思是微软没有容忍不符合RFC 822中的httpHeader必须以CRLF结束的规定的服务器响应。 

一个解决方案是在application.config或web.config文件里加入 

  <system.net> 

    <settings> 

      <httpWebRequest useUnsafeHeaderParsing="true" /> 

    </settings> 

  </system.net> 

允许系统容忍(tolerant)只以CR或LF结尾的hearder信息 

 

 

using System.Reflection;


bool GetAllowUnsafeHeaderParsing() { //Get the assembly that contains the internal class Assembly aNetAssembly = 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", BindingFlags.Static | BindingFlags.GetProperty | 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 FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField( "useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance); if (aUseUnsafeHeaderParsing != null) return (bool)aUseUnsafeHeaderParsing.GetValue(anInstance); } } } return false; } void SetAllowUnsafeHeaderParsing(bool val) { //Get the assembly that contains the internal class Assembly aNetAssembly = 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", BindingFlags.Static | BindingFlags.GetProperty | 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 FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField( "useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance); if (aUseUnsafeHeaderParsing != null) { aUseUnsafeHeaderParsing.SetValue(anInstance, val); } } } } }

 

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

标签:setting   提交   static   http   ant   bre   seh   unsafe   web   

原文地址:http://www.cnblogs.com/chengulv/p/6146533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!