2011-12-07 47 views
14

我收到以下例外情況:如何在代碼中設置useUnsafeHeaderParsing

服務器提交協議違規。第= ResponseHeader詳細= CR必須跟有LF

從這個問題:

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

我明白,我需要設置useUnsafeHeaderParsing爲True。

這裏是我的代碼:

 HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url); 
     WebResponse myResp = myReq.GetResponse(); //exception is thrown here 

useUnsafeHeaderParsing是HttpWebRequestElement類的屬性。

如何將它集成到上面的代碼中?

非常感謝!

回答

27

你需要設置這是你的web.config,裏面<system.net>部分,像這樣:

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

如果由於某種原因,你不希望你的配置做到這一點,你可以做它通過編程設置您的配置設置。一個例子見this page

+0

如何將此代碼添加到C#Web窗體應用程序「的app.config」? – TheMuyu

23

正如埃德溫指出你需要設置useUnsafeHeaderParsing屬性在你的web.config或app.config文件。如果您確實想在運行時動態更改該值,則必須採取反射措施,因爲該值被掩埋在System.Net.Configuration.SettingsSectionInternal的實例中,並且不能公開訪問。

下面是一個代碼示例(基於信息發現here),做的伎倆:

using System; 
using System.Net; 
using System.Net.Configuration; 
using System.Reflection; 

namespace UnsafeHeaderParsingSample 
{ 
    class Program 
    { 
     static void Main() 
     { 
      // Enable UseUnsafeHeaderParsing 
      if (!ToggleAllowUnsafeHeaderParsing(true)) 
      { 
       // Couldn't set flag. Log the fact, throw an exception or whatever. 
      } 

      // This request will now allow unsafe header parsing, i.e. GetResponse won't throw an exception. 
      var request = (HttpWebRequest) WebRequest.Create("http://localhost:8000"); 
      var response = request.GetResponse(); 

      // Disable UseUnsafeHeaderParsing 
      if (!ToggleAllowUnsafeHeaderParsing(false)) 
      { 
       // Couldn't change flag. Log the fact, throw an exception or whatever. 
      } 

      // This request won't allow unsafe header parsing, i.e. GetResponse will throw an exception. 
      var strictHeaderRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8000"); 
      var strictResponse = strictHeaderRequest.GetResponse(); 
     } 

     // Enable/disable useUnsafeHeaderParsing. 
     // See http://o2platform.wordpress.com/2010/10/20/dealing-with-the-server-committed-a-protocol-violation-sectionresponsestatusline/ 
     public static bool ToggleAllowUnsafeHeaderParsing(bool enable) 
     { 
      //Get the assembly that contains the internal class 
      Assembly assembly = Assembly.GetAssembly(typeof(SettingsSection)); 
      if (assembly != null) 
      { 
       //Use the assembly in order to get the internal type for the internal class 
       Type settingsSectionType = assembly.GetType("System.Net.Configuration.SettingsSectionInternal"); 
       if (settingsSectionType != null) 
       { 
        //Use the internal static property to get an instance of the internal settings class. 
        //If the static instance isn't created already invoking the property will create it for us. 
        object anInstance = settingsSectionType.InvokeMember("Section", 
        BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { }); 
        if (anInstance != null) 
        { 
         //Locate the private bool field that tells the framework if unsafe header parsing is allowed 
         FieldInfo aUseUnsafeHeaderParsing = settingsSectionType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance); 
         if (aUseUnsafeHeaderParsing != null) 
         { 
          aUseUnsafeHeaderParsing.SetValue(anInstance, enable); 
          return true; 
         } 

        } 
       } 
      } 
      return false; 
     } 
    } 
} 
+1

這就是我所需要的。謝謝! – MatBee

+0

許多用戶執行我的遊戲啓動器和更新程序,但其中一半用與OP相同的消息使用它。你的回答解決了這個問題,非常感謝:D – G4BB3R

+0

六年後,它仍然有用。謝謝! –

相關問題