2011-12-07 25 views
14

Tôi nhận được ngoại lệ sau:cách đặt useUnsafeHeaderParsing in code

Máy chủ vi phạm giao thức. Section = ResponseHeader chi tiết = CR phải được theo sau bởi LF

Từ câu hỏi này:

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

Tôi hiểu rằng tôi cần phải đặt useUnsafeHeaderParsing True.

Đây là mã của tôi:

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

useUnsafeHeaderParsing là một tài sản của HttpWebRequestElement lớp.

Làm cách nào để tích hợp nó trong mã ở trên?

Rất cám ơn!

Trả lời

27

Bạn cần phải thiết lập này là trong web.config của bạn, bên trong <system.net> phần, như thế này:

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

Nếu vì một lý do nào, bạn không muốn làm điều đó từ cấu hình của bạn, bạn có thể làm nó từ mã bằng cách thiết lập cấu hình cài đặt cấu hình của bạn theo trình tự. Xem this page để biết ví dụ.

+0

cách mã này thêm vào "app.config" trong C# Webform ứng dụng? – TheMuyu

23

Vì Edwin đã chỉ ra rằng bạn cần đặt thuộc tính useUnsafeHeaderParsing trong tệp web.config hoặc app.config của mình. Nếu bạn thực sự muốn thay đổi giá trị động tại thời gian chạy, thì bạn sẽ phải sử dụng để phản ánh khi giá trị được chôn trong một phiên bản System.Net.Configuration.SettingsSectionInternal và không thể truy cập công khai.

Dưới đây là một ví dụ mã (dựa trên các thông tin tìm thấy here) mà hiện các trick:

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

Đây là những gì tôi cần. CẢM ƠN BẠN! – MatBee

+0

Nhiều người dùng đã thực thi trình khởi chạy trò chơi và trình cập nhật của tôi nhưng một nửa trong số họ có lỗi khi sử dụng nó với cùng một thông báo như OP. Câu trả lời của bạn đã khắc phục được sự cố, cảm ơn rất nhiều: D – G4BB3R

+0

Sáu năm sau nó vẫn hữu ích. Cảm ơn! –