加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

调用Https WebService是报“基础连接已经关闭: 未能为 SSL/TLS

发布时间:2020-12-17 02:08:00 所属栏目:安全 来源:网络整理
导读:今天在通过C#访问Webservice时遇到一个问题,首先通过对方提供的wsdl生成了调用代理类,在测试能否正常访问时,访问正式环境https://api.xxx.xx,一切正常,当访问测试环境是https://apitest.xxx.xxx,总是报“基础连接已经关闭: 未能为 SSL/TLS 安全通道建

今天在通过C#访问Webservice时遇到一个问题,首先通过对方提供的wsdl生成了调用代理类,在测试能否正常访问时,访问正式环境https://api.xxx.xx,一切正常,当访问测试环境是https://apitest.xxx.xxx,总是报“基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系”? InnerException信息为:根据验证过程,远程证书无效。

?

在网上找到解决方法:

http://social.microsoft.com/Forums/zh-CN/wcfzhchs/thread/1591a00d-d431-4ad8-bbd5-34950c39d563

?

依照上面的描述操作,问题解决,以作纪念

?

分以下三步:

1. 添加引用

????using System.Net;
??? using System.Net.Security;
??? using System.Security.Authentication;
??? using System.Security.Cryptography.X509Certificates;

?

2. 在生成的代理类中添加RemoteCertificateValidate函数

??? private static bool RemoteCertificateValidate(object sender,X509Certificate cert,

??????????????????????????????????????????????????????????????????????????X509Chain chain,SslPolicyErrors error){
??????????? // trust any certificate!!!
??????????? System.Console.WriteLine("Warning,trust any certificate");
??????????? //为了通过证书验证,总是返回true
??????????? return true;
????}

?

3. 在生产的代理类的构造函数中添加

??? ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;//验证服务器证书回调自动验证

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读