基础连接已经关闭: 未能为 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;
}
}
}

如图:


原网址: 访问
创建于: 2023-05-23 15:47:48
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论