基礎連接已經關閉: 未能為 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;
}
}
}

 

如圖:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM