如何在代码中设置useUnsafeHeaderParsing

时间:2011-12-07 23:28:02

标签: c# c#-4.0 httpwebrequest httpwebresponse

我收到以下异常:

服务器提交了协议违规。 Section = ResponseHeader Detail = CR必须后跟LF

从这个问题:

  

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

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

这是我的代码:

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

useUnsafeHeaderParsing是HttpWebRequestElement类的属性。

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

3 个答案:

答案 0 :(得分:31)

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

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

如果出于某种原因,您不希望从配置中执行此操作,则可以通过以编程方式设置配置设置从代码执行此操作。有关示例,请参阅this page

答案 1 :(得分:26)

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

这是一个代码示例(基于找到的信息here),可以解决这个问题:

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

答案 2 :(得分:0)

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

google-api-core==1.7.0
google-auth==1.6.2
google-cloud-core==0.29.1
google-cloud-storage==1.14.0
google-resumable-media==0.3.2
googleapis-common-protos==1.5.6
protobuf==3.6.1