Задайте вопросЗадайте вопрос
 

ОтвеченоHow do I enable useUnsafeHeaderParsing from code? (.NET 2.0)

  • 14 марта 2006 г. 11:56UncleRedZ Медали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     

    Hi,

    We have several webservers that do not follow the correct RFC specification when performing some specific HTTP web requests, in order to communicate with them we need to enable unsafe header parsing. Changeing the webservers to respond correctly is NOT an option in our case.

    According to documentation, adding the following lines to the app.config enables the unsafe header parsing.

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

    This works nicely for regular applications, however, the problem is that our .NET code is exposed and run as a COM object. And adding a .config file to .NET assemblies doesn't work, neither when being loaded by COM or if the .NET assembly is loaded and used from another .NET application.

    Since the application that loads our .NET COM object isn't a .NET application it can not have it's own app.config file.

    So how do I enable unsafe header parsing in code?

    In .NET 1.1 the unsafe header parsing flag could be changed using reflection, however this flag does not appear to be in the same place in .NET 2.0.

    Cheers,
    RedZ

Ответы

  • 14 марта 2006 г. 14:56UncleRedZ Медали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     Отвечено

    Ok, I figured it out. It can be done in a similar way as with .NET 1.1. However it would be a lot nicer and safer if there had been a public API for doing this in code, instead of using reflection.

    A lot of people have to deal with a large number of legacy systems that can't be corrected to use the proper RFC HTTP responses and for them this is a real issue. Idealy the use of safe or unsafe parsing should be configurable in the HTTP request and not on a global level.

    Anyway, here's the code for hacking/fixing it in .NET 2.0

    (Add System.Configuration as a reference to your project.)

    public static bool SetAllowUnsafeHeaderParsing20()
    {
      //Get the assembly that contains the internal class
      Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
      if (aNetAssembly != null)
      {
        //Use the assembly in order to get the internal type for the internal class
        Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
        if (aSettingsType != null)
        {
          //Use the internal static property to get an instance of the internal settings class.
          //If the static instance isn't created allready the property will create it for us.
          object anInstance = aSettingsType.InvokeMember("Section",
            BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { });

          if (anInstance != null)
          {
            //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
            FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
            if (aUseUnsafeHeaderParsing != null)
            {
              aUseUnsafeHeaderParsing.SetValue(anInstance, true);
              return true;
            }
          }
        }
      }
      return false;
    }

    Cheers,
    RedZ

Все ответы