調用 Https WebService 使用程序自動生成代理類

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

2 在瀏覽器裏打開這個地址,會顯示一個XML,右擊另存爲1.wsdl文件api

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 安全通道創建信任關係 。這個是由於證書問題。工具

在生成的代理類的構造方法裏面加入  回調驗證,基本上就能夠無視證書了this

///<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裏面的方法spa

 string inLicense ="";//受權文件3d

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

相關文章
相關標籤/搜索