今天在通过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;//验证服务器证书回调自动验证
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。