1. 程式人生 > >關於 服務器提交了協議沖突. Section=ResponseHeader Detail=CR 後面必須是 LF 錯誤

關於 服務器提交了協議沖突. Section=ResponseHeader Detail=CR 後面必須是 LF 錯誤

.config dex log 之前 fig 添加 code for 設定

用WebClient 去下載數據時發現有服務器提交了協議沖突. Section=ResponseHeader Detail=CR 後面必須是 LF錯誤,解決辦法

1.在app.config種添加 web 在 web.config種添加

<system.net>

<settings>

<httpWebRequest useUnsafeHeaderParsing="true" />

</settings>

</system.net> 2.以反射的形式在程序中設定useUnsafeHeaderParsing的值【該方法須在發送請求之前進行設定否則無效果】
public static bool SetAllowUnsafeHeaderParsing(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; }

以上摘抄於http://www.cnblogs.com/lewisli/p/6775010.html

關於 服務器提交了協議沖突. Section=ResponseHeader Detail=CR 後面必須是 LF 錯誤