访问https接口报错 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系
在网上查阅了资料找到了解决方案,大致如下
调用http访问前加上这句话
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy(); internal class AcceptAllCertificatePolicy : ICertificatePolicy { public AcceptAllCertificatePolicy() { } public bool CheckValidationResult(ServicePoint sPoint, X509Certificate cert, WebRequest wRequest, int certProb) { // Always accept return true; } }
这么写有点麻烦,但是能解决问题
查看了ServerCertificateValidationCallback属性,发现它的类型为RemoteCertificateValidationCallback的委托,为何不直接写一个参数相同的方法直接返回true呢
public delegate bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors);
解决方案
ServicePointManager.ServerCertificateValidationCallback = Callback; private static bool Callback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }