这周在做一个项目需要向外部单位的一个平台推送信息,,对方平台是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 = "?wsdl";
string uid = "";
string Password = "123456";
//通过结构函数实例化 来传值
RequestSOAPHeader = new RequestSOAPHeader(uid, ucode);
}
/// <remarks/>
public event receivePubCompletedEventHandler receivePublishCompleted;
/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestNamespace = "", ResponseNamespace = "",
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引用的要领去挪用接口。