example data create c# c#-4.0 httpwebrequest httpwebresponse

data - webrequest post parameters c#



cómo configurar useUnsafeHeaderParsing en el código (2)

Recibo la siguiente excepción:

El servidor ha ocasionado una violación del protocolo. Section = ResponseHeader Detail = CR debe ser seguido por LF

De esta pregunta:

HttpWebRequestError: el servidor cometió una violación de protocolo. Section = ResponseHeader Detail = CR debe ser seguido por LF

Entiendo que necesito establecer useUnsafeHeaderParsing en True.

Aquí está mi código:

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

useUnsafeHeaderParsing es una propiedad de la clase HttpWebRequestElement.

¿Cómo lo integro en el código anterior?

¡Muchas gracias!


Debes configurar esto en tu web.config, dentro de la sección <system.net> , así:

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

Si, por alguna razón, no desea hacerlo desde su configuración, puede hacerlo desde el código estableciendo de manera prógma la configuración de su configuración. Vea esta página para un ejemplo.


Como ha señalado Edwin, debe establecer el atributo useUnsafeHeaderParsing en su archivo web.config o app.config. Si realmente desea cambiar el valor dinámicamente en el tiempo de ejecución, deberá recurrir a la reflexión ya que el valor está oculto en una instancia de System.Net.Configuration.SettingsSectionInternal y no accesible públicamente.

Aquí hay un ejemplo de código (basado en la información que se encuentra aquí ) que hace el truco:

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; } } }