2011-12-07 6 views
14

मैं निम्नलिखित अपवाद हो रही है में useUnsafeHeaderParsing सेट करने का तरीका:कोड

सर्वर को उस प्रोटोकॉल उल्लंघन प्रतिबद्ध है। धारा = ResponseHeader विस्तार = सीआर वामो

इस सवाल से द्वारा पालन किया जाना चाहिए:

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

आप स्थापित करने के लिए यह आपके web.config में है, <system.net> खंड के अंदर की जरूरत है, इस तरह:

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

, तो किसी कारण से, आप अपना config से यह करना चाहते हैं, तुम कर सकते हो यह आपकी कॉन्फ़िगरेशन सेटिंग्स को प्राग्रामेटिक रूप से सेट करके कोड से। उदाहरण के लिए this page देखें।

+0

कैसे इस कोड को जोड़ना "app.config" ग # वेबफ़ॉर्म आवेदन में करने के लिए? – TheMuyu

23

जैसा कि एडविन ने इंगित किया है कि आपको अपने web.config या app.config फ़ाइल में उपयोग UnnsfeHeaderParsing विशेषता सेट करने की आवश्यकता है। यदि आप वास्तव में रनटाइम पर गतिशील रूप से मूल्य बदलना चाहते हैं, तो आपको प्रतिबिंब का सहारा लेना होगा क्योंकि मान 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

कई उपयोगकर्ताओं ने मेरे गेम लॉन्चर और अपडेटर को निष्पादित किया लेकिन उनमें से आधे में ओपी के समान संदेश के साथ इसका उपयोग करने में त्रुटियां थीं। आपके उत्तर ने समस्या को ठीक किया, बहुत बहुत धन्यवाद: डी – G4BB3R

+0

छह साल बाद यह अभी भी उपयोगी है। धन्यवाद! –