首页 文章

从 Postman 和 Visual Studio Webtest 调用 REST API

提问于
浏览
1

我面临一个奇怪的问题,我能够从 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

连线:keep-alive

授权:基本

Cache-Control:no-cache

User-Agent:Mozilla/5.0(Windows NT 10.0; Win64; x64)AppleWebKit/537.36(KHTML,与 Gecko 一样)Chrome/54.0.2840.71 Safari/537.36

Postman-Token:4dfaa309-c7d8-6785-d59c-9679ad4f3aaa

接受:/

Accept-Encoding:gzip,deflate,sdch,br

Accept-Language:en-US,en; q=0.8

然而,当我从 Webtest 调用相同的 rest API 时,得到以下错误

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

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

任何想法都赞赏。

谢谢

1 回答

  • 2

    实际上找到了根本原因,为什么呼叫从 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;
            }
        }
    }
    

相关问题