2011-12-07 30 views
14

次の例外が発生します。コードでuseUnsafeHeaderParsingを設定する方法

サーバーがプロトコル違反をコミットしました。セクション= ResponseHeader詳細= CRはこの質問からLF

が続かなければなりません:私はTrueにuseUnsafeHeaderParsing設定する必要があることを理解

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

。ここで

私のコードです:

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

useUnsafeHeaderParsingはHttpWebRequestElementクラスのプロパティです。

私はどのように上記のコードに統合しますか?

多くの感謝!

答えて

27

あなたはこのように、<system.net>セクションの内側に、これはあなたのweb.configファイルにある設定する必要があります。何らかの理由で、あなたの設定からそれを行うにはしたくない、場合

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

、あなたが行うことができますあなたの設定をプログラム的に設定することでコードから例については、this pageを参照してください。

+0

このコードはC#のWebフォームアプリケーションで「app.configを」に追加する方法を? – TheMuyu

23

Edwinが指摘したように、web.configファイルまたはapp.configファイルでuseUnsafeHeaderParsing属性を設定する必要があります。実行時に値を動的に変更したい場合は、値が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

6年後もまだ役に立ちます。ありがとう! –

関連する問題