zoukankan      html  css  js  c++  java
  • 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。 根据验证过程,远程证书无效------解决方法

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

     InnerException = {"根据验证过程,远程证书无效。"}

    解决方法如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Net;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;

    namespace EvaluationAndWitnessTestDemo
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void Button1_Click(object sender, EventArgs e)
    {
    try
    {
    #region 第一种方式,远程文件,文件http下载

    //string path = @"http://localhost:8080/Source/XMLConfig.xml";
    //XmlDocument doc = new XmlDocument();
    //doc.Load(path);
    //XmlNode httpservice = doc.SelectSingleNode("configuration");
    //XmlNodeList httpserviceNodes = httpservice.ChildNodes;
    #endregion

    #region 第二种方式,本地文件
    //string path = AppDomain.CurrentDomain.BaseDirectory;
    //path = Path.Combine(path, "XMLConfig.xml");
    //XmlDocument doc = new XmlDocument();
    //doc.Load(path);
    //XmlNode httpservice = doc.SelectSingleNode("configuration");
    //XmlNodeList httpserviceNodes = httpservice.ChildNodes;
    #endregion

    #region MyRegion
    string path = @"http://localhost:8080/Source/XMLConfig.xml";

    XmlDocument doc = new XmlDocument();
    doc.Load(new System.Net.WebClient().DownloadString(path));
    XmlNode httpservice = doc.SelectSingleNode("configuration");
    XmlNodeList httpserviceNodes = httpservice.ChildNodes;
    #endregion

    //上述是http,不是https,如果是用https的话,由于没有证书,会报错:Message = "基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。" InnerException = {"根据验证过程,远程证书无效。"}
    //解决方法:button2_Click
    }
    catch (Exception ex)
    {
    System.Console.WriteLine(ex.Message);
    }
    }

    private void Button2_Click(object sender, EventArgs e)
    {

    SetCertificatePolicy();//https请求时会报错,如下图
    string path = @"https://localhost:448/Source/XMLConfig.xml";
    XmlDocument doc = new XmlDocument();
    doc.Load(path);
    XmlNode httpservice = doc.SelectSingleNode("configuration");
    XmlNodeList httpserviceNodes = httpservice.ChildNodes;
    foreach (XmlNode item in httpserviceNodes)
    {
    string Host = item.SelectSingleNode("host").InnerText.Trim();
    Console.WriteLine($"Host:{Host}");
    }

    //下面的WebRequest也是一样的由于没有证书,报错Message = "基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。" InnerException = {"根据验证过程,远程证书无效。"}
    //WebResponse response = WebRequest.Create(@"https://localhost:448/Source/XMLConfig.xml").GetResponse();
    //Stream streamReceive = response.GetResponseStream();
    //Encoding encoding = Encoding.UTF8;

    //StreamReader streamReader = new StreamReader(streamReceive, encoding);
    //string strResult = streamReader.ReadToEnd();
    //streamReceive.Dispose();
    //streamReader.Dispose();
    //Console.WriteLine(strResult);
    }


    /// <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;
    }
    }
    }

    如图:

  • 相关阅读:
    原型链加强练习
    Javascript中的原型链,__proto__和prototype等问题总结
    HTTPS 到底加密了什么?
    PrismCDN 网络的架构解析,以及低延迟、低成本的奥秘
    取代 FlashP2P,H5P2P 将成为 WebP2P 主流
    低延时的P2P HLS直播技术实践
    深挖“窄带高清”的实现原理
    【省带宽、压成本专题】爱奇艺第一季度又烧了11个亿元,什么时候是个头?
    【省带宽、压成本专题】深入解析 H.265 编码模式,带你了解 Apple 全面推进 H.265 的原因
    让互联网更快,Server Push 特性及开启方式详解
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/10219758.html
Copyright © 2011-2022 走看看