from http://blog.csdn.net/lbuskeep/article/details/39184019
今天在通过C#访问webservice时遇到一个问题,首先通过对方提供的wsdl生成了调用代理类,在测试能否正常访问时,本机调试(http协议)一切正常,当访问正式环境时(https协议),总是报“ 基础连接已经关闭: 未能为 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) { System.Console.WriteLine("Warning, trust any certificate" ); return true ; }
3.在生成的代理类的构造函数中添加
//验证服务器证书回调
ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;