调用 Https WebService 使用程序自动生成代理类

2022-12-09,,,,

1 商家提供的WebService接口:  https://ws.nciic.org.cn/nciic_ws/services/NciicServices?wsdl

2 在浏览器里打开这个地址,会显示一个XML,右击另存为1.wsdl文件

3 使用vs 的 wsdl.exe工具的来生成代理

wsdl.exe的位置  C:\Program Files\\Microsoft SDKs\Windows\v6.0A\bin\wsdl.exe  (视个人情况而定)

说明一下:WebService地址 可以是 http或者https的域名,可以是C:\1.WSDL的本地文件。 本文就是使用的本地文件(第2步保存的 1.wsdl文件)

4 使用的Https地址,有时会报:基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系 。这个是因为证书问题。

在生成的代理类的构造方法里面加入  回调验证,基本上就可以无视证书了

///<remarks/>
public nciicGetCondition()
{
this.Url = "http://api.nciic.org.cn/nciic_ws/services/nciicGetCondition"; //验证服务器证书回调自动验证
ServicePointManager.ServerCertificateValidationCallback += 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;
}

5  调用WebService里面的方法

string inLicense ="";//授权文件

NciicServices objText = new NciicServices();

//读XML文件
string inConditions = File.ReadAllText(HttpRuntime.AppDomainAppPath + "\\XMLFile1.xml");

string r = objText.nciicCheck(inLicense, inConditions);

Response.Write(r);

最后要感谢  苏飞博客

本文引用:http://www.cnblogs.com/sufei/archive/2010/03/14/https.html

调用 Https WebService 使用程序自动生成代理类的相关教程结束。

《调用 Https WebService 使用程序自动生成代理类.doc》

下载本文的Word格式文档,以方便收藏与打印。