2011-12-07 13 views
14

Ricevo la seguente eccezione:come impostare useUnsafeHeaderParsing nel codice

Il server ha commesso una violazione del protocollo. Sezione = ResponseHeader Particolare = CR deve essere seguito da LF

Da questa domanda:

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

ho capito che ho bisogno di impostare useUnsafeHeaderParsing su True.

Ecco il mio codice:

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

useUnsafeHeaderParsing è una proprietà di classe di HttpWebRequestElement.

Come si integra nel codice sopra?

Grazie mille!

risposta

27

È necessario impostare questo è nel web.config, all'interno <system.net> sezione, come questo:

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

Se, per qualche motivo, non vuoi farlo dal config, che si possa fare dal codice impostando prgrammaticamente le impostazioni di configurazione. Vedere this page per un esempio.

+0

come questo codice aggiungi a "app.config" in C# applicazione modulo web? – TheMuyu

23

Come indicato da Edwin, è necessario impostare l'attributo useUnsafeHeaderParsing nel file web.config o app.config. Se si desidera modificare il valore dinamicamente in fase di esecuzione, sarà necessario ricorrere alla riflessione poiché il valore è nascosto in un'istanza di System.Net.Configuration.SettingsSectionInternal e non accessibile pubblicamente.

Ecco un esempio di codice (sulla base di informazioni trovato here), che fa il trucco:

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

Questo è ciò di cui avevo bisogno. GRAZIE! – MatBee

+0

Molti utenti hanno eseguito il mio launcher e il programma di aggiornamento, ma la metà di loro aveva errori che lo utilizzavano con lo stesso messaggio di OP. La tua risposta ha risolto il problema, grazie mille: D – G4BB3R

+0

Sei anni dopo è ancora utile. Grazie! –

Problemi correlati