服务器提交了协议冲突. Section=ResponseHeader Detail=“Content-Length”标题值无效

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了服务器提交了协议冲突. Section=ResponseHeader Detail=“Content-Length”标题值无效相关的知识,希望对你有一定的参考价值。

C# 采集金碧坊网站数据时出现该异常,导致无法获取网页源码
The server committed a protocol violation. Section=ResponseHeader Detail='Content-Length' header value is invalid

参考技术A 网上大部分都是在app.Config配置里设置useUnsafeHeaderParsing:

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

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

One of the issues involving XML-RPC.NET that turns up fairly frequently is when the library throws an instance of System.Net.WebException with the message ""The server committed a protocol violation". This usually occurs because from .NET 1.1 SP1 onwards the parsing of HTTP responses became much more strict, as a security measure to prevent attacks which exploit malformed HTTP status lines and headers. The strict behaviour can be switched off via the application config file:

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

From .NET 2.0 this behaviour can be configured programmatically using the HttpWebRequestElement useUnsafeHeaderParsing property. At first when I read about this I assumed it was a property that can be set dynamically at runtime, for example in the same way as the HttpWebRequest KeepAlive property. But in fact its used in the new 2.0 configuration infrastructure to set the value in the config file (although once you do this the new value applies to the current running application as well as any apps launched afterwards. The new configuration infrastructure is pretty complex but this code seems to work ok:

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

ConfigurationUserLevel.None specifies that the configuration file in the same directory as the executable should be modified so this file has to be writable. The other options PerUserRoaming and PerUserRoamingAndLocal can be used in different scenarios.
Finally, I found the code below in a post on the .NET Framework Networking and Communication forum. This uses reflection to set the private field useUnsafeHeaderParsing to true and as a result may not be suitable in all scenarios where the relevant code access security permission is not available. (Note: add System.Configuration.dll as a reference to your project.)

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;
本回答被提问者采纳

C# SOAP Web 服务 服务器违反了协议。 Section=ResponseStatusLine

【中文标题】C# SOAP Web 服务 服务器违反了协议。 Section=ResponseStatusLine【英文标题】:C# SOAP Web Service The server committed a protocol violation. Section=ResponseStatusLine 【发布时间】:2012-10-16 22:18:57 【问题描述】:

我有一个调用 Web 服务的应用程序...我收到一个错误,我正在拔掉我头上剩下的几根头发,也许有人可以帮忙。

代码如下:

Service_RetrieveIntervalDataserviceagent srv = new Service_RetrieveIntervalDataserviceagent();
srv.Credentials = new NetworkCredential(UserName, Password, Domain);
MDMIntervalDataInput dataInput = new MDMIntervalDataInput();
 string Url = srv.Url;
DeviceList deviceList = new DeviceList();

deviceList.Type = DeviceListType.M;
deviceList.DeviceId = "2862,2876,2877".Split(',');

//Setup dataInput
dataInput.ApplicationName = "TestApp";
dataInput.StartDate = DateTime.Now.AddDays(-3).ToString("MM/dd/yyyy");
dataInput.EndDate = DateTime.Now.AddDays(-3).ToString("MM/dd/yyyy");
dataInput.OutputMode = MDMIntervalDataInputOutputMode.Wire;
dataInput.DeviceList = deviceList;
srv.RetrieveIntervalData_V10(dataInput);

我不断收到错误:

服务器违反了协议。 Section=ResponseStatusLine

我确实注意到 URL 从 HTTPS 更改为 HTTP 这可能是问题吗?

我也尝试将以下内容添加到我的配置文件中,但它仍然不起作用:

<system.net> 
  <settings> 
    <httpWebRequest useUnsafeHeaderParsing="true" /> 
  </settings> 
</system.net> 

我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

这是我修复它的方法...

我注意到 URL 正在从 HTTPS 更改为 HTTP...HTTP URL 是罪魁祸首,因此我将其更新为手动设置 URL,并且它起作用了。

【讨论】:

以上是关于服务器提交了协议冲突. Section=ResponseHeader Detail=“Content-Length”标题值无效的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

服务器提交了协议违规。 Section = ResponseStatusLine“对于PHP webservice

C#发送邮件错误: System.net.mail.smtpException;服务器提交了协议冲突 服务器响应为: .

c# winform httpWebResponse post出错