post请求远程url 报错“基础连接已经关闭...Authentication.AuthenticationException...远程证书无效”解决方案

当我们有时用代码编写post请求url远程地址会报“基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。 ---> System.Security.Authentication.AuthenticationException: 根据验证过程,远程证书无效。”这个异常,是因为远程url使用的域名 没有购买证书,所以用以下方式来解决:

ps:在create url之前 设定“获取或设置用于验证服务器证书的回调”永远为true 即可,具体如下

post请求一定需要:System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);

在这句代码前加上: ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;即可

RemoteCertificateValidate事件代码如下:

private static bool RemoteCertificateValidate(object sender, X509Certificate cert,X509Chain chain, SslPolicyErrors error)
        {
            //为了通过证书验证,总是返回true
            return true;
        }

搞定

原文地址:https://www.cnblogs.com/yonguibe/p/4294234.html