C#访问https的webservice

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.添加引用

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using System.Net;  
  2. using System.Net.Security;  
  3. using System.Security.Authentication;  
  4. using System.Security.Cryptography.X509Certificates;  
  5. 2.在生成的代理类中添加RemoteCertificateValidate函数  
  6. private static bool RemoteCertificateValidate(object sender, X509Certificate cert,X509Chain chain, SslPolicyErrors error)  
  7. {  
  8.     System.Console.WriteLine("Warning, trust any certificate");  
  9.     //为了通过证书验证,总是返回true  
  10.     return true;  
  11. }  
3.在生成的代理类的构造函数中添加
//验证服务器证书回调

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值