1、 VS新建解決方案,通過WSDL生成CS文件
2、 新建空白XML文件,將以下文件復制到空白xml文件中,保存到D盤,如:d:\wsdlconfig.xml
c# Soap true false properties oldAsync
3、通過VS自帶的WSDL程序,生成文件,方法如下:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\wsdl.exe /par:d:\wsdlconfig.xml http://hostname:port/xxxService?wsdl
4、 將生成好的文件放cs放到項目中,<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD48cD41oaLQwr2o0ru49sDgvMyz0FNvYXBIZWFkZXKjrMjno7pMaW1zU29hcEhlYWRlcjwvcD48L2Jsb2NrcXVvdGU+PHByZSBjbGFzcz0="brush:java;"> public string username { get; set; } public string password { get; set; } ///
/// 構造函數 /// public LimsSoapHeader() { } ////// 構造函數 /// /// 用戶名 /// 密碼 public LimsSoapHeader(string userName, string passWord) { this.username = userName; this.password = passWord; }
6、在通過WSDL生成的類裡添加 對soapHeader引用,先申請對應的SoapHeader變量,並在方法裡添加引用。
public LimsSoapHeader limsSoapHeader; [System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace="http://webservice.web.lims.panda.com/", ResponseNamespace="http://webservice.web.lims.panda.com/", Use=System.Web.Services.Description.SoapBindingUse.Literal)] [return: System.Xml.Serialization.XmlElementAttribute("return")] [SoapHeader("limsSoapHeader")] public string importPhysicalData(string xml) { object[] results = this.Invoke("importPhysicalData", new object[] { xml}); return ((string)(results[0])); }
在調用方法之前,指定對應的soapHeader即可,如
HospitalFacadeServiceImplService proxy = new HospitalFacadeServiceImplService(); proxy.Url = "http://hostname:port/xxxService"; //指定SoapHeader驗證 proxy.limsSoapHeader = new LimsSoapHeader("soapUserName", "soapPassword"); String xml = proxy.importPhysicalData(" my name is panda"); Console.WriteLine(xml);
這樣就完成了C#對帶有SoapHeader驗證的WebService調用。