C#调用带有SOAPHeader说明

引用设置

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

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

      

  xmlns='http://microsoft.com/webReference/'>
      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放到项目中,

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

    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调用。

你可能感兴趣的:(vs2008)