2011-12-07 3 views
14

다음 예외가 발생합니다.코드에서 useUnsafeHeaderParsing을 설정하는 방법

서버가 프로토콜 위반을 커밋했습니다. 제 = ResponseHeader 상세 = CR은이 질문에서

LF

다음에해야

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

는 내가 참으로 useUnsafeHeaderParsing 설정할 필요가 있음을 이해합니다.

 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#을 웹 양식 응용 프로그램 "의 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 년 후에도 여전히 유용합니다. 감사! –

관련 문제