XmlDocument.Load(url) url是https远程时,报错" 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。" "根据验证过程,远程证书无效。"
XmlDocument.Load(url) url是https远程时,报错" 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。" "根据验证过程,远程证书无效。"
XmlDocument.Load(url) url是是本地文件或者和是http时不会报错,但是如果是https远程时,会报错" 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。" "根据验证过程,远程证书无效。"
原因是因为没有证书,证书是无效的,解决方法如下:
1、添加三个引用:using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates;
2、添加下面两个方法SetCertificatePolicy,事件RemoteCertificateValidate
/// <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)
{
System.Console.WriteLine("Warning, trust any certificate");
return true;
}
只要在XmlDocument.Load(url)之前加上 SetCertificatePolicy(),就ok。