当前位置:   article > 正文

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

 

如图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/689515
推荐阅读
相关标签
  

闽ICP备14008679号