无法理解 SOAP 头

问题描述:

      java客户端调用.net发布的web service(需要soap头验证)时发生无法理解SOAP头的错误。

重现步骤:

     1.用eclipse自带的web service client生成工具根据wsdl生成客户端(利用axis创建)。

     2.在java中调用web service接口。

wsdl文档头消息描述

<s:complexType name="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>

报错信息:无法理解SOAP头。

问题原因:axis生成的客户端代码一般是不加soap消息头的,这个需要我们手动添加。

解决方法:

      给***SoapStub.java的protected org.apache.axis.client.Call createCall()方法添加如下代码

    protected org.apache.axis.client.Call createCall() throws java.rmi.RemoteException {
        try {
            org.apache.axis.client.Call _call = super._createCall();
            
            SOAPHeaderElement head = new SOAPHeaderElement(new PrefixedQName(new javax.xml.namespace.QName("命名空间", "MySoapHeader")));
            head.setActor(null);
            try{
            head.addChildElement("UserName").addTextNode("用户名");
            head.addChildElement("PassWord").addTextNode("密码");
            head.setMustUnderstand(false);
            }catch(Exception e)
            {
            	System.out.println(" soapheader Exception == "); 
            	e.printStackTrace();
            }
            _call.addHeader(head);
            
            if (super.maintainSessionSet) {
                _call.setMaintainSession(super.maintainSession);
            }

 over!

猜你喜欢

转载自zhaobing315.iteye.com/blog/1101127