当前位置 博文首页 > 文章内容

    基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。 根据验证过程,远程证书无效------解决方法...:weixin_30651273的博客

    作者:shunshunshun18 栏目:未分类 时间:2021-10-17 20:57:19

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    推荐 IIS7批量FTP管理工具 IIS7批量FTP管理
    IIS7批量FTP管理功能说明:
    1、可批量导入,导出FTP信息
    2、其他ftp工具有的功能,我们也有
    3、特色功能:可以定时上传下载
    4、数据信息列表化、一眼就能知道那个是那个
    5、批量连接 标签页式切换 方便快捷
    6、7大连接模式 更多好的兼容
    7、内嵌编辑器 有效解决普通txt记事本乱码
    8、锁屏功能 当程序有规定时间内没人操作,则自动锁程序。输入密码才可以正常操作

    本产品适用于:懒得记录FTP信息和有批量定时备份,上传下载的运维或站长。

    图:


    IIS7批量FTP管理

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

     

    如图:

    cs