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();



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值