C#运行出现:基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。

12 篇文章 1 订阅

英文:The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel
使用HttpWebRequest 访问 https://mapi.alipay.com/gateway.do?..支付宝接口时 在本机WIN10 64位环境 完全没问题,使用firefox,IE Edge打开也没问题,但是在win2003 server 上报错:基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系,用IE 无法打开链接

,如果在win2003 上使用fiddler 打开链接会弹出对话框提示:

Session #8: The remote server (mapi.alipay.com) presented a certificate that did not validate, due to RemoteCertificateChainErrors.

0 - 无法验证证书的签名。。。如果忽略错误则可正常访问。

原因:证书没官方签名?

We checked the credentials passed; it seems everything was fine. But still it was failing whenever we make the request to the server with the above same message. When we checked their environment, we found customer uses the self-signed certificate on the server. This is because, by default, .NET checks whether SSL certificates are signed by a certificate from the Trusted Root Certificate store.
近期在使用RestSharp进行http请求时遇到这个错误,网上了解一些相关原因,大部分该问题都是证书问题引起的,就想我们在使用chrome浏览器打开某一网站时提醒该网站不是一个安全网址,可能存在被攻击的情况一下,需要我们进入高级选项手动进入该网址,在使用c#发送http请求时遇到该问题原因基本一致,想了解具体问题的可以搜一下x.509证书。

再次记录一下解决问题的办法

///
/// 设置证书安全性
///
private static void SetCertificatePolicy()
{
ervicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
}

///
/// 远程证书验证
///
private static bool RemoteCertificateValidate(object sender, X509Certificate cert,X509Chain chain, SslPolicyErrors error)
{
return true;
}
发送Http请求前先请求一下SetCertificatePolicy()方法,将该网站的证书安全性验证设置为true即可解决该问题
实际如下;

在这里插入图片描述

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光怪陆离的节日

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值