How does one set Expect100Continue to false on a Coded Visual Studio 2013 WebTestRequest?

时间:2015-09-14 16:16:03

标签: c# visual-studio protocols performance-testing

I am currently having an issue where when attempting to hit an external service, the webtest is throwing the following exception:

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

Investigation into this led me to this excellent article describing the underlying cause of this exception.

http://mehdi.me/a-tale-of-debugging-the-linkedin-api-net-and-http-protocol-violations/

Taking his repo program and adding request.ServicePoint.Expect100Continue = false; into the BuildWebRequest method and pointing it at the external service we need to test does indeed, weirdly as he pointed out, result in success.

However, the coded web tests do not use the normal WebRequest object, but instead the WebTestRequest project which does not have a settable Expect100Continue field nor seem to respected ANY change to ServicePointManger.Expect100Continue.

My question is thus: how does on set this on the WebTestRequest object type? Is it possible or will some kind of wrapper be necessary?

1 个答案:

答案 0 :(得分:0)

最后我们找不到设置Expect100Continue标志的方法,作为解决方法,并且必须启用UnsafeHeaderFlag。

由于我们无法使用http配置或app配置来获取负载测试解决方案,因此我们必须使用反射来设置UnsafeHeader标志值。

    public static bool SetAllowUnsafeHeaderParsing20(bool value)
    {
        //Get the assembly that contains the internal class
        var aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));

        if (aNetAssembly == null) return false;

        //Use the assembly in order to get the internal type for the internal class
        var aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");

        if (aSettingsType == null) return false;

        //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.
        var anInstance = aSettingsType.InvokeMember("Section",
                                                     BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic,
                                                     null,
                                                     null, 
                                                     new object[] { });
        if (anInstance == null) return false;

        //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
        var aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);

        if (aUseUnsafeHeaderParsing == null) return false;

        aUseUnsafeHeaderParsing.SetValue(anInstance, value);

        return true;
    }

然后我们使用了一个webtest插件来确保为给定的webtest设置这个。

    public override void PreWebTest(object sender, PreWebTestEventArgs e)
    {
        UnsafeHeaderParsing.SetAllowUnsafeHeaderParsing20(UseUnsafeHeaderParsing);
    }

这导致解析响应而不会导致异常。请注意,它确实掩盖了原始问题,但这只能通过服务器发送符合规范的信息来解决。