这周在做一个项目需要向外部单位的一个平台推送信息,对方平台是JAVA平台开发,需要提供一个SOAP的header 合法性验证信息,否则接口返回的信息是header 为空。
谈谈我的这一周的经验总结:
第一步:生成WSDL的代理类,不要用WEB 引用的方法去调用接口。
怎么生成WSDL的代理类呢?
菜单:工具 ==》外部工具
命令行:C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\wsdl.exe
点击确定保存以后,在工具菜单里会多出一个WSDL生成代理类。
参数里 /l:cs /out: 你的WebService的接口地址,点击确定会自动生成一个代理类。
第二步:新建RequestSOAPHeader类
记住这个类一定要继承SoapHeader类,新建对方接口需要验证的SOAP 参数。
新建一个带参数的构造函数RequestSOAPHeader(string s_uid,string s_ucode)
/// <summary> /// RequestSOAPHeader 的摘要说明 /// </summary> public class RequestSOAPHeader:SoapHeader { public string uid{ get;set; } public string ucode{ get;set; } public RequestSOAPHeader() { } public RequestSOAPHeader(string s_uid,string s_ucode) { this.uid = s_uid; this.ucode = s_ucode; } }
第三步:在代理类上添加SOAPHeader的声明
public RequestSOAPHeader RequestSOAPHeader;
public partial class ReceivePubServiceImplService : System.Web.Services.Protocols.SoapHttpClientProtocol { private System.Threading.SendOrPostCallback receivePublishOperationCompleted; public RequestSOAPHeader RequestSOAPHeader; /// <remarks/> public ReceivePubServiceImplService() { //接口地址 this.Url = "http://xxxxxx.com/platform/services/ReceivePublishService?wsdl"; string uid = ""; string Password = "123456"; //通过构造函数实例化 来传值 RequestSOAPHeader = new RequestSOAPHeader(uid, ucode); } /// <remarks/> public event receivePubCompletedEventHandler receivePublishCompleted; /// <remarks/> [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestNamespace = "http://server.webService.com/", ResponseNamespace = "http://server.webService.com/",
Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] [return: System.Xml.Serialization.XmlElementAttribute("return", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = true)] [SoapHeader("RequestSOAPHeader")] public string receivePub([System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = true)] string arg0) { object[] results = this.Invoke("receivePub", new object[] { arg0}); return ((string)(results[0])); }
加红色的一定要添加。
最后实例化方法
ReceivePubServiceImplService t = new ReceivePubServiceImplService();
t.receivePub(str);//信息报送成功。
记住不要通过WEB引用的方法去调用接口。