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
    评论
C#中动态调用WebService有多种方式。其中一种方式是使用SoapHttpClientProtocol类。这种方式需要将添加WebService引用生成的Reference.cs类中的服务接口集成到自己定义的服务调用类中,而且服务调用类需要继承自SoapHttpClientProtocol。如果服务接口发生了改变,需要修改服务调用类。以下是一个示例代码: ```csharp public class MySoapHttpClientProtocol : SoapHttpClientProtocol { public MySoapHttpClientProtocol(string url) { Url = url; } [SoapHeader("ClientContext")] [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestNamespace = MyNamespace, ResponseNamespace = MyNamespace, Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] [return: System.Xml.Serialization.XmlElementAttribute("out", IsNullable = true)] public string myMethod([System.Xml.Serialization.XmlElementAttribute(IsNullable = true)] string in0) { try { object[] results = this.Invoke("getConnection", new object[] { in0 }); return ((string)(results<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* *2* [C# 动态调用WebService](https://blog.csdn.net/chthq/article/details/19628775)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值