根据客户端程序语言及调用方式不同,设置的方法也不同,下面示例说明客户端程序语言为java调用方式为动态调用的设置方法:用org.apache.axis.client.Call 的addHeader方法:
call.addHeader(new SOAPHeaderElement("Authorization","username",username));
call.addHeader(new SOAPHeaderElement("Authorization","password",password));  
其他的调用方式及其他语言设置方式请查阅Axis相关文档..请问大侠,,c#.net中怎么调用呢??没有有类似的方法?我已经建立了一个 webservice引用了。。可是不知道怎么设置soapheader,他们把身份验证都放在 soapheader里了。

解决方案 »

  1.   

    http://www.cnblogs.com/RainWaterLily/archive/2008/01/27/1055099.html
      

  2.   

    跟他给的java例子是一样的吧   
    实例webservice的时候进行身份验证
    他应该把这个写在构造函数里了
      

  3.   

    网上例子挺多的.http://www.codeproject.com/info/search.aspx?artkw=web+service
      

  4.   

    我用过最简单的,添加web引用后,直接就可以用了
    比如命名空间和类为A和B
    则可以A.B operate=new A.b();
    operate.方法()
    那个operate可以自己换url
      

  5.   

    help!!!是直接可以用,可是人家要求身份认证,需要写入soapHeader头部
      

  6.   

    [align=left] localhost.Service service = new WSDemoClient.localhost.Service();            // 在客户端实例 MySoapHeader 对象
                service.MySoapHeaderValue = new WSDemoClient.localhost.MySoapHeader();
                service.MySoapHeaderValue.Username = "a";
                service.MySoapHeaderValue.Password = "b";
                service.CookieContainer = new System.Net.CookieContainer();            service.Login("a", "a");
    align]
      

  7.   

    服务端:
    public MySoapHeader header;
    // 自定义的 SOAPHeader
    public class MySoapHeader : System.Web.Services.Protocols.SoapHeader
    {
        public string Username;
        public string Password;
    }在方法名上添加[System.Web.Services.Protocols.SoapHeader("header")]
      

  8.   

    请问,你的MySoapHeaderValue 是自定义属性吗的吗?如果是自定义的,那么头部添加的实现在那个地方?
      

  9.   

    我用的也是5楼的方法。
    soapHeader头部 
    7楼的你试试吧。
      

  10.   

    服务端:using System;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Service : System.Web.Services.WebService
    {    // 自定义的 MySoapHeader 类型的数据成员 
        
        public MySoapHeader header;
            public Service () {        //如果使用设计的组件,请取消注释以下行 
            //InitializeComponent(); 
        }
        [WebMethod(EnableSession=true)]
        [System.Web.Services.Protocols.SoapHeader("header")]
        public void Login(string username, string password)
        {
            if( this.CheckUser( username, password))
            {
                // set id
                this.Session["Login"] = "Ok";
            }
        }    [System.Web.Services.Protocols.SoapHeader("header")]
        [WebMethod(EnableSession=true)]
        public string HelloWorld() {
            object o = this.Session["Login"];
            if (o != null)
            {
                return "Hello World";
            }
            else
            {
                throw new Exception("no access");
            }
        }    [WebMethod(EnableSession=true)]
        public DateTime GetServerTime()
        {
            object o = this.Session["Login"];
            if (o!=null)
            {
                System.Threading.Thread.Sleep(4000);
                return DateTime.Now;
            }
            else
            {
                throw new Exception("no access");
            }
        }
       
        private bool CheckUser(string username, string password)
        {
            if (username == password)
                return true;
            else
                return false;
        }}// 自定义的 SOAPHeader
    public class MySoapHeader : System.Web.Services.Protocols.SoapHeader
    {
        public string Username;
        public string Password;
    }
    客户端using System;
    using System.Collections.Generic;
    using System.Text;namespace WSDemoClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                localhost.Service service = new WSDemoClient.localhost.Service();            string url = service.Url;            Console.WriteLine(url);            // 在客户端实例 MySoapHeader 对象
                service.MySoapHeaderValue = new WSDemoClient.localhost.MySoapHeader();
                service.MySoapHeaderValue.Username = "a";
                service.MySoapHeaderValue.Password = "b";
               
                service.CookieContainer = new System.Net.CookieContainer();            service.Login("a", "a");
                
                string hello = service.HelloWorld();            Console.WriteLine(hello);            DateTime now = service.GetServerTime();            Console.WriteLine(now);            Console.ReadLine();
            }
        }
    }
      

  11.   

    服务端是JAVA写的,具体代码不知道 ,他只提供了 JAVA的调用方法啊
      

  12.   

    服务端应该提供了继承自 System.Web.WebServices.SoapHeader 的自定义 SoapHeader 的类型吧
      

  13.   

    从那里看出来? wsdl文件中可以看出来吗?
      

  14.   

    可以
     <s:element name="MySoapHeader" type="tns:MySoapHeader" />
          <s:complexType name="MySoapHeader">  //这个是有MySoapHeader类
            <s:sequence>
              <s:element minOccurs="0" maxOccurs="1" name="Username" type="s:string" />
              <s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string" />
            </s:sequence>
            <s:anyAttribute />
          </s:complexType>//Login方法上添加了   [System.Web.Services.Protocols.SoapHeader("header")]
      <wsdl:message name="LoginMySoapHeader">
        <wsdl:part name="MySoapHeader" element="tns:MySoapHeader" />
      </wsdl:message>
    对比看下吧
      

  15.   

     <wsdl:operation name="Login">
          <soap12:operation soapAction="http://tempuri.org/Login" style="document" />
          <wsdl:input>
            <soap12:body use="literal" />
            <soap12:header message="tns:LoginMySoapHeader" part="MySoapHeader" use="literal" />      </wsdl:input>
          <wsdl:output>
            <soap12:body use="literal" />
          </wsdl:output>
        </wsdl:operation>
      

  16.   

    我看来看去,还是看不出webservice服务端有定义头部啊。。大虾帮看看:
    <?xml version="1.0" encoding="utf-8"?>
    <wsdl:definitions xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:intf="http://server.webservice.core.epm" xmlns:impl="http://server.webservice.core.epm" targetNamespace="http://server.webservice.core.epm" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
      <wsdl:types />
      <wsdl:message name="originalInvokeRequest">
        <wsdl:part name="path" type="xsd:string" />
        <wsdl:part name="methodName" type="xsd:string" />
        <wsdl:part name="dataXmlStr" type="xsd:string" />
      </wsdl:message>
      <wsdl:message name="invokeResponse">
        <wsdl:part name="invokeReturn" type="xsd:string" />
      </wsdl:message>
      <wsdl:message name="invokeRequest">
        <wsdl:part name="path" type="xsd:string" />
        <wsdl:part name="methodName" type="xsd:string" />
        <wsdl:part name="dataXmlStr" type="xsd:string" />
      </wsdl:message>
      <wsdl:message name="originalInvokeResponse">
        <wsdl:part name="originalInvokeReturn" type="xsd:anyType" />
      </wsdl:message>
      <wsdl:message name="invokeServiceResponse">
        <wsdl:part name="invokeServiceReturn" type="xsd:string" />
      </wsdl:message>
      <wsdl:message name="invokeServiceRequest">
        <wsdl:part name="path" type="xsd:string" />
        <wsdl:part name="methodName" type="xsd:string" />
        <wsdl:part name="dataXmlStr" type="xsd:string" />
      </wsdl:message>
      <wsdl:portType name="GenericServer">
        <wsdl:operation name="invoke" parameterOrder="path methodName dataXmlStr">
          <wsdl:input name="invokeRequest" message="impl:invokeRequest" />
          <wsdl:output name="invokeResponse" message="impl:invokeResponse" />
        </wsdl:operation>
        <wsdl:operation name="invokeService" parameterOrder="path methodName dataXmlStr">
          <wsdl:input name="invokeServiceRequest" message="impl:invokeServiceRequest" />
          <wsdl:output name="invokeServiceResponse" message="impl:invokeServiceResponse" />
        </wsdl:operation>
        <wsdl:operation name="originalInvoke" parameterOrder="path methodName dataXmlStr">
          <wsdl:input name="originalInvokeRequest" message="impl:originalInvokeRequest" />
          <wsdl:output name="originalInvokeResponse" message="impl:originalInvokeResponse" />
        </wsdl:operation>
      </wsdl:portType>
      <wsdl:binding name="GenericServerSoapBinding" type="impl:GenericServer">
        <wsdlsoap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" />
        <wsdl:operation name="invoke">
          <wsdlsoap:operation soapAction="" />
          <wsdl:input name="invokeRequest">
            <wsdlsoap:body use="encoded" namespace="http://server.webservice.core.epm" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
          </wsdl:input>
          <wsdl:output name="invokeResponse">
            <wsdlsoap:body use="encoded" namespace="http://server.webservice.core.epm" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
          </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="invokeService">
          <wsdlsoap:operation soapAction="" />
          <wsdl:input name="invokeServiceRequest">
            <wsdlsoap:body use="encoded" namespace="http://server.webservice.core.epm" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
          </wsdl:input>
          <wsdl:output name="invokeServiceResponse">
            <wsdlsoap:body use="encoded" namespace="http://server.webservice.core.epm" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
          </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="originalInvoke">
          <wsdlsoap:operation soapAction="" />
          <wsdl:input name="originalInvokeRequest">
            <wsdlsoap:body use="encoded" namespace="http://server.webservice.core.epm" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
          </wsdl:input>
          <wsdl:output name="originalInvokeResponse">
            <wsdlsoap:body use="encoded" namespace="http://server.webservice.core.epm" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
          </wsdl:output>
        </wsdl:operation>
      </wsdl:binding>
      <wsdl:service name="GenericServerService">
        <wsdl:port name="GenericServer" binding="impl:GenericServerSoapBinding">
          <wsdlsoap:address location="http://172.19.4.170:7010/web/services/GenericServer" />
        </wsdl:port>
      </wsdl:service>
    </wsdl:definitions>以上是 GenericServer.wsdl的代码。。他要求的最终 传输的SOAP头信息如下:
    最终传输的SOAP头信息如下:
    <soapenv:Header>
    <ns1:username
    soapenv:actor="http://schemas.xmlsoap.org/soap/actor/nex"
    soapenv:mustUnderstand="0" xsi:type="soapenc:string"
    xmlns:ns1="Authorization" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
    username
    </ns1:username>
    <ns2:password
        soapenv:actor="http://schemas.xmlsoap.org/soap/actor/nex"
        soapenv:mustUnderstand="0" xsi:type="soapenc:string"       xmlns:ns2="Authorization"                                                     xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
    password
    </ns2:password>
    </soapenv:Header>
      

  17.   

    一样的问题,估计是同一个接口,soap头格式都一样。有没有高手啊,指教
      

  18.   

    我也在做一个MIS接口项目,接口文档和这个一模一样,是不是同一家公司的啊,我的QQ号414835697
      

  19.   

    同求同求啊,也是这个厂家的,查了好久没找到怎么弄呀~~!
    楼主,搞定了么?  还有 zhouguofang1981 这个你搞定了么?