共用方式為


Visual Studio WebTest Fails with error "Request failed: Received an unexpected EOF or 0 bytes from the transport stream"

Here is a solution to a problem that some folks have encountered running Visual Studio WebTests using HTTPS protocols. Some times there maybe a device (Load Balancer, Firewall, or Application Applicance) that does not support TLS (Transport Layer Security). Usually if this issue is present you will get an error message: Request Failed: Received an unexpected EOF or 0 bytes from the transport stream.

So the default in Visual Studio is to use TLS, and not the downlevel SSLv3, but the question may come up , what do we do if we are stuck using a device that requires SSLv3? Well, the extensibility aspects of Visual Studio Test capability are there to help us fix this up. The following sample WebTest plugin shows you how you can override the default TLS. Hope this code sample helps you out if you.

The following code sample shows how to specify SSL vs. TLS in your WebTests by taking this code, put in a class file, and compile in your test project. Then you would right-click on your WebTest in the Visual Studio design view, and add WebTest Plugin using this plugin.

 using System;

using System.ComponentModel;

using System.Net;

using System.Net.Security;

using System.Security.Cryptography.X509Certificates;

using Microsoft.VisualStudio.TestTools.WebTesting;





namespace ExperimentalTesting

{

    [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 SSLv3ForcedPlugin : 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);



            // We're using SSL3 here and not TLS. Without this line, nothing works.

            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;

        }

    }

}