日期:2014-05-18  浏览次数:21143 次

关于webservice,在asp.net页面调用总会报服务器提交了协议冲突
在asp.net页面调用webservice总会报服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 L的错误。
今天找了一天答案了也没有解决了。求各位帮助

------解决方案--------------------
一般在遇到论坛的POST提交的情况下,
如果论坛所返回的response带有CR LF之类的行结尾,
.net 1.1和.net 2.0便会抛出异常,解决方法最简单当然是下面第一条,
但如果想动态装载该设置的话,必须用第三条的代码。




一个频繁(但合法的)发生的涉及XML-RPC.NET的问题是,运行库抛出System.Net.WebException异常,提示信息是“服务器提交了协议冲突”。
发生这个异常是因为.NET v1.1 sp1向前解析HTTP响应(response)变得更加严格,作为防止那些使用非格式化的HTTP状态行和头部信息来攻击的安全标准。
这个严格的规定可以通过应用程序配置文件(app.config)来关闭:

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


从.NET 2.0开始,这个规定可以用代码来配置,利用HttpWebRequestElement类的useUnsafeHeaderParsing属性。
一开始我读这个的时,我断定这个属性可以在运行时动态设置,例如像HttpWebRequest类的KeepAlive属性一样。
但实际上她被用在新2.0的配置基础结构中,需要在配置文件中设置这个属性的值(虽然一旦你这样做这个值会应该到当然前运行程序,也会应用到之后运行的程序)。
这个新的配置基础结构是比较复杂的但以下代码貌似可行:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
SettingsSection section = (SettingsSection)config.GetSection("system.net/settings");
section.HttpWebRequest.UseUnsafeHeaderParsing = false;config.Save();


ConfigurationUserLevel.None规定该配置文件必须在运行文件(exe)的同一目录下,且运行文件需可修改的,所以配置也必须是可写的。
另外两个选项PerUserRoaming和PerUserRoamingAndLocal可以被用于不用的特定情况下。

最后,我在一个.NET框架的网络传输和通讯论坛的一张帖子中发现以下这段代码。
代码用了反射来设置私有(private)选项useUnsafeHeaderParsing为true,结果可能不一定能适应所有特定情况,
例如那些相关代码不能进入安全许可的特定情况(注意:要添加System.Configuration.dll到你的工程项目的引用)

public static bool SetAllowUnsafeHeaderParsing()
{
//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, true);
return true;
}
}
}
}
return false;
}


------解决方案--------------------
主体意思是微软没有容忍不符合RFC 822中的httpHeader必须以CRLF结束的规定的服务器响应。 
一个解决方案是在application.config或web.config文件里加入 
<system.net> 
<settings> 
<httpWebRequest useUnsafeHeaderParsing="true" /> 
</settings> 
</system.net> 
允许系统容忍(tolerant)只以CR或LF结尾的hearder信息 

------解决方案--------------------
在application.config或web.config文件里加入
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true">
</httpWebRequest>

</settings>

</system.net>

 winform当然是放在 app.config里边了
但是对放法没有说