Windows 服务应用程序中的 useUnsafeHeaderParsing

0 c#

我有一个 VS2005 Windows 服务,需要根据 MSDN 文档使用“useUnsafeHeaderParsing”。

由于这是我的 Windows 服务中使用的库,因此我没有 web.config 来添加 httpwebrequest 元素并将 useUnsafeHeaderParsing 设置为 true。

我将如何在代码中实现这一目标。我尝试了这个这个,但那是行不通的。

Aar*_*web 5

如果您愿意,您可以通过编程方式启用 unsafeHeaderParsing - 这是我在Quick and Dirty Feed Parser中包含的用于打开该功能的函数- 它类似于您在问题中链接到的一些函数:

private static bool SetUseUnsafeHeaderParsing(bool b)
    {
        Assembly a = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
        if (a == null) return false;

        Type t = a.GetType("System.Net.Configuration.SettingsSectionInternal");
        if (t == null) return false;

        object o = t.InvokeMember("Section",
            BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { });
        if (o == null) return false;

        FieldInfo f = t.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
        if (f == null) return false;

        f.SetValue(o, b);

        return true;
    }
Run Code Online (Sandbox Code Playgroud)

我已经在生产代码中使用了它,并且无论您部署在什么环境中,它都可以正常工作 - 只要可以通过 GAC 中的某些程序集访问 System.Net.Configuration 命名空间,就可以开始使用。