如何在代码中设置useUnsafeHeaderParsing

发布于 2024-12-20 08:39:33 字数 640 浏览 0 评论 0原文

我收到以下异常:

服务器违反了协议。部分 = ResponseHeader 详细信息 = CR 后面必须跟有 LF

从这个问题:

HttpWebRequestError:服务器违反了协议。 Section=ResponseHeader Detail=CR 后必须跟 LF

我知道我需要将 useUnsafeHeaderParsing 设置为 True。

这是我的代码:

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

useUnsafeHeaderParsing 是 HttpWebRequestElement 类的属性。

如何将其集成到上面的代码中?

I am getting the following exception:

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

From this question:

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

I understand that I need to set useUnsafeHeaderParsing to True.

Here is my code:

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

useUnsafeHeaderParsing is a property of HttpWebRequestElement class.

How do I integrate it in the above code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林很绿却致人迷途 2024-12-27 08:39:33

您需要在 web.config 的 部分中进行设置,如下所示:

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

如果出于某种原因,您不想从配置中执行此操作,您可以通过以编程方式设置配置设置,从代码中完成此操作。请参阅此页面 举个例子。

You need to set this is in your web.config, inside <system.net> section, like this:

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

If, for some reason, you do not want to do it from your config, you could do it from code by prgrammatically setting your config settings. See this page for an example.

和我恋爱吧 2024-12-27 08:39:33

正如 Edwin 指出的,您需要在 web.config 或 app.config 文件中设置 useUnsafeHeaderParsing 属性。如果您确实想在运行时动态更改该值,则必须求助于反射,因为该值隐藏在 System.Net.Configuration.SettingsSectionInternal 实例中并且无法公开访问。

这是一个代码示例(基于找到的信息

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

As Edwin has pointed out you need to set the useUnsafeHeaderParsing attribute in your web.config or app.config file. If you really want to change the value dynamically at runtime, then you'll have to resort to reflection as the value is buried in an instance of System.Net.Configuration.SettingsSectionInternal and not publicly accessible.

Here is a code example (based on the info found here) that does the trick:

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;
        }
    }
}
二手情话 2024-12-27 08:39:33

如果您不想使用反射,您可以尝试以下代码(System.Configuration.dll参考):

using System.Configuration;
using System.Net.Configuration;

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = (SettingsSection)config.GetSection("system.net/settings");
settings.HttpWebRequest.UseUnsafeHeaderParsing = true;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("system.net/settings");

If you did't want use Reflection, you can try this code (System.Configuration.dll reference):

using System.Configuration;
using System.Net.Configuration;

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = (SettingsSection)config.GetSection("system.net/settings");
settings.HttpWebRequest.UseUnsafeHeaderParsing = true;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("system.net/settings");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文