C# 基础链接已经关闭: 未能为 SSL/TLS 安全通道创建信任关系。

今天写程序的时候调用到一个第三方的DLL文件,本机调试一切都正常,可是程序不是到服务器之后一直提示一个BUG:"基础链接已经关闭: 未能为SSL/TLS 安全通道创建信任关系"。
后来把DLL文件进行反编译,发现是在得到请求的时候出错了。 安全

引用  WebResponse response = WebRequest.Create("https://……").GetResponse();

定义一个类,来对远程X.509证书的验证,进行处理,返回为true.咱们要本身定义一个类,而后在客户单调用WCF服务以前,执行一次便可。代码以下:服务器

 

 public static class Util
    {
        /// <summary>
        /// Sets the cert policy.
        /// </summary>
        public static void SetCertificatePolicy()
        {
            ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
        }

        /// <summary>
        /// Remotes the certificate validate.
        /// </summary>
        private static bool RemoteCertificateValidate(
           object sender, X509Certificate cert,
            X509Chain chain, SslPolicyErrors error)
        {
            // trust any certificate!!!
            System.Console.WriteLine("Warning, trust any certificate");
            return true;
        }
    }

在WebRequest.Create("https://……").GetResponse();调用操做点前先调用这个方法: Util.SetCertificatePolicy();spa

相关文章
相关标签/搜索