【问题标题】:Calling REST API from Postman vs Visual Studio Webtest从 Postman 与 Visual Studio Webtest 调用 REST API
【发布时间】:2017-03-11 16:09:04
【问题描述】:

我遇到了一个奇怪的问题,我能够从 POSTMAN 调用 (https://my.factcorp.com/ABCorp/Reporting/api/Events/) Rest API,但不能从 Visual Studio Web Test 调用。

PostMan 呼叫跟踪

GET https://my.factcorp.com/ABCorp/Reporting/api/Events/HTTP/1.1

主机:my.factcorp.com

连接:保持活动

授权:基本

缓存控制:无缓存

用户代理:Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,如 Gecko)Chrome/54.0.2840.71 Safari/537.36

邮递员令牌:4dfaa309-c7d8-6785-d59c-9679ad4f3aaa

接受:/

接受编码:gzip、deflate、sdch、br

接受语言:en-US,en;q=0.8

然而,当我从 Webtest 调用相同的 REST API 时,出现以下错误

请求失败:现有连接被远程主机强行关闭

我可以看到 Postman 在请求中添加了一些额外的标头,我也尝试通过手动添加所有这些标头。

任何想法表示赞赏。

谢谢

【问题讨论】:

    标签: visual-studio postman webtest


    【解决方案1】:

    实际上找到了根本原因,为什么调用从 Postman 而不是从 VS WebTest 成功。

    原因是 Postman 足够聪明,可以将请求安全性更正为 (TLS/1.2)

    而 WebTest 无法做到这一点,因为它使用较低级别的 System.Net 协议。

    现在要解决这个问题,我们实际上可以编写自定义 WebTest 插件,它可以覆盖默认的安全行为。

    using System;
    using System.ComponentModel;
    using System.Net;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    using Microsoft.VisualStudio.TestTools.WebTesting;
    
    namespace MyWebTest
    {
        [Description("This plugin will force the underlying System.Net ServicePointManager to negotiate downlevel SSLv3 instead of TLS. WARNING: The servers X509 Certificate will be ignored as part of this process, so verify that you are testing the correct system.")]
        public class TLS12ForcedPlugin : WebTestPlugin
        {
    
            [Description("Enable or Disable the plugin functionality")]
            [DefaultValue(true)]
            public bool Enabled { get; set; }
    
            public override void PreWebTest(object sender, PreWebTestEventArgs e)
            {
                base.PreWebTest(sender, e);
    
                //For TLS
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    
                //For SSL
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
    
                //we wire up the callback so we can override  behavior and force it to accept the cert
                ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCB;
    
                //let them know we made changes to the service point manager
                e.WebTest.AddCommentToResult(this.ToString() + " has made the following modification-> ServicePointManager.SecurityProtocol set to use SSLv3 in WebTest Plugin.");
            }
            public static bool RemoteCertificateValidationCB(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            {
                //If it is really important, validate the certificate issuer here.
                //this will accept any certificate
                return true;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多