System.Net.WebException: 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。 ---> System.Security.Authentication.AuthenticationException: 根据验证过程,远程证书无效。

今天写程序的时候调用到一个第三方提供的https地址,访问此地址去获取加密的json格式数据,出现BUG

c#报错 :  System.Net.WebException: 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。 ---> System.Security.Authentication.AuthenticationException: 根据验证过程,远程证书无效。

引用:

private string callbackRefund(string url, string data)
{

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  request.Method = "POST";

  byte[] bytes = UTF8Encoding.UTF8.GetBytes(data);
  request.ContentLength = bytes.Length;
  request.ContentType= "application/json";
  long x_ts = GetCurrentTimeUnix();
  request.Headers.Add("x-ts", x_ts.ToString());
  request.Headers.Add("x-hospitalId", "40617");
  request.Headers.Add("x-sig", "kinyer_debug_sign");
  using (Stream requestStream = request.GetRequestStream())
  {
    foreach (byte b in bytes)
    {
      requestStream.WriteByte(b);
    }
  }

  using (WebResponse response = request.GetResponse())
  {
    using (Stream responseStream = response.GetResponseStream())
    {
      byte[] responseContent;
      using (MemoryStream ms = new MemoryStream())
      {
        responseStream.CopyTo(ms);
        responseContent = ms.ToArray();
      }
    return UTF8Encoding.UTF8.GetString(responseContent);
    }
  }

}

解决方法:

步骤一:定义一个类,来对远程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;
        }
    }

步骤二:   你要在HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);调用操作点前先调用这个方法: Util.SetCertificatePolicy();

这样实现了远程访问https地址   项目部署在win2012 2R上可用

原文地址:https://www.cnblogs.com/bzsz-quan/p/9897979.html

时间: 2024-08-25 19:07:03

System.Net.WebException: 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。 ---> System.Security.Authentication.AuthenticationException: 根据验证过程,远程证书无效。的相关文章

https调用出现【基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系】错误

今天不知道为什么网站访问就出现问题了,惊得我一头汗,别是我昨天打了log4,就影响今天的内容了,后来发现,确实不是,是写入流失败了,报[基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系]错误,后来让总线查,他们说是自己的接口链接证书失效了,正在补救. 病急乱投医中网上查到解决方法:重写通信通道连接验证回调 直接上代码: 1 先加入命名空间: 2 3 using System.Net.Security; 4 using System.Security.Authentication;

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

今天写程序的时候调用到一个第三方的DLL文件,本机调试一切都正常,但是程序不是到服务器以后一直提示一个BUG:"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系". 后来把DLL文件进行反编译,发现是在获得请求的时候出错了. 引用 WebResponse response = WebRequest.Create("https://……").GetResponse(); 定义一个类,来对远程X.509证书的验证,进行处理,返回为true.我们要自己定义一

解决Error"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系

今天写程序的时候调用到一个第三方的DLL文件,本机调试一切都正常,但是程序不是到服务器以后一直提示一个BUG:"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系". 后来把DLL文件进行反编译,发现是在获得请求的时候出错了. 引用 WebResponse response = WebRequest.Create("https://--").GetResponse(); 于是在服务器上用浏览器打开上面的地址,发现会弹出一个确认证书的窗口,看来是证书问题.

基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系

#region private static bool ValidateServerCertificate 解决Error"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系." /// <summary> /// 解决Error"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系." /// </summary> /// <param name="sender"></param&

C#动态调用webService出现 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。

在远程调用WebService时出现下面的情况: 这种情况一般是由于证书有问题时出现,如果在浏览器中打开是这种情况: 解决方法新建一个类,代码如下: 1 public static class CertificateTrust 2 { 3 4 public static void SetCertificatePolicy() 5 { 6 ServicePointManager.ServerCertificateValidationCallback 7 += RemoteCertificateVa

基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系

//1. ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; //2. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback ( delegate { return true; } )

[转][C#]基础连接已经关闭 未能为 SSL/TLS 安全通道建立信任关系

来自:https://www.cnblogs.com/waw/p/8286608.html 代码部分: static TestApplication(){ ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate; } private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X

C# 访问https 未能创建 SSL/TLS 安全通道

C# 访问https请求被中止: 未能创建 SSL/TLS 安全通道(Could not create SSL/TLS secure channel) 一般GetResponse可以直接访问https,如果不行添加回调: ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult

【SSL】WebClient 请求 https 页面出错:未能创建 SSL/TLS 安全通道

#问题: 当向一个https的url上发送请求,报错:未能创建 SSL/TLS 安全通道: using (WebClient client = new WebClient()) { string address="https://xxx.com"; client.Headers.Add(HttpRequestHeader.ContentType, "text/xml"); System.Text.ASCIIEncoding encoding=new System.T