当前位置: 代码迷 >> XML/SOAP >> C#调用含有SOAPHeader说明
  详细解决方案

C#调用含有SOAPHeader说明

热度:166   发布时间:2016-05-23 01:56:07.0
C#调用带有SOAPHeader说明

引用设置

1、 VS新建解决方案,通过WSDL生成CS文件

2、 新建空白XML文件,将以下文件复制到空白xml文件中,保存到D盘,如:d:\wsdlconfig.xml

      

 <wsdlParameters xmlns='http://microsoft.com/webReference/'>      <language>c#</language>        <protocol>Soap</protocol>        <nologo>true</nologo>        <sharetypes>false</sharetypes>        <webReferenceOptions>          <codeGenerationOptions>properties oldAsync</codeGenerationOptions>        </webReferenceOptions>      </wsdlParameters>

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放到项目中,

5、新建一个类继承SoapHeader,如:LimsSoapHeader

    public string username { get; set; }        public string password { get; set; }        /// <summary>        /// 构造函数        /// </summary>        public LimsSoapHeader() { } /// <summary>        /// 构造函数        /// </summary>        /// <param name="userName">用户名</param>        /// <param name="passWord">密码</param>        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调用。

  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!
 
  • 最近登录:Mon Aug 21 18:21:53 CST 2017
  • 最近登录:Mon Aug 21 18:21:53 CST 2017
  • 最近登录:Mon Aug 21 18:21:53 CST 2017
  • 最近登录:Mon Aug 21 18:21:53 CST 2017
  • 最近登录:Mon Aug 21 18:21:53 CST 2017