如何在axis1中为SOAP Header设置名称空间

时间:2018-06-12 11:45:22

标签: java web-services axis soap-client

我使用java axis1来调用.net soap web服务,但我得到一个例外:

An exception of type 'System.ServiceModel.MessageHeaderException' occurred and was caught.
Type : System.ServiceModel.MessageHeaderException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : No Action header was found with namespace 'http://www.w3.org/2005/08/addressing' for the given message.
Source : System.ServiceModel
Help link : 
HeaderName : Action
HeaderNamespace : http://www.w3.org/2005/08/addressing
Stack Trace :    at System.ServiceModel.Dispatcher.ChannelHandler.ReplyContractFilterDidNotMatch(RequestContext request)
   at System.ServiceModel.Dispatcher.ChannelHandler.EnsureChannelAndEndpoint(RequestContext request)
   at System.ServiceModel.Dispatcher.ChannelHandler.TryRetrievingInstanceContextCore(RequestContext request)

我尝试使用SoapUI-5.4.0,它可以顺利进行,下面是SoapUI请求格式:

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
    <s:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
        <CustomHeader xmlns="http://schemas.test.com/CustomHeader">
            <UserName>abc</UserName>
            <Password>pwd</Password>
        </CustomHeader>
        <wsa:Action>http://www.test.com/DemoInterface/SendReport</wsa:Action>
    </s:Header>
    <s:Body>
        <SendReport xmlns="http://www.test.com/">
            <xml>
                <![CDATA[ xml content ]]>
            </xml>
        </SendReport>
    </s:Body>
</s:Envelope>

我的java代码如下。但无论我如何尝试,WCF总是像第1节一样捕获异常。我不知道如何使用命名空间&#34; http://www.w3.org/2005/08/addressing&#34;来解决Action标头。我是java的新手,我的专业知识都是关于C#的。感谢是否有人可以帮助我解决这个java问题。

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
import org.apache.axis.client.*;
import org.apache.axis.client.Call;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.axis.soap.SOAPConstants;
import java.net.*;

public class WebServiceDemo_axis {

    public static void main(String[] args) {

        try {

            Service service = new Service();
            Call call = (Call)service.createCall();

            call.setTargetEndpointAddress(new URL("https://localhost/UnitTest/TestDemo.svc?wsdl"));
            call.setSOAPActionURI("http://www.test.com/DemoInterface/SendReport");
            call.setSOAPVersion(SOAPConstants.SOAP12_CONSTANTS);
            call.setOperationName(new QName("http://www.test.com/", "SendReport"));
            call.setUseSOAPAction(true);
            call.setReturnType(XMLType.SOAP_STRING);
            call.setEncodingStyle("UTF-8");
            call.addParameter(new QName("http://www.test.com/", "xml"), XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);

            SOAPHeaderElement hd = new SOAPHeaderElement(new QName("http://schemas.test.com/CustomHeader", "CustomHeader"));
            hd.setMustUnderstand(true);
            hd.addChildElement("UserName").addTextNode("abc");
            hd.addChildElement("Password").addTextNode("pwd");
            call.addHeader(hd);
            System.out.println(hd.toString());

            String opt = (String)call.invoke(new Object[] { "xml content for testing" });

            if (opt != null)
                System.out.println(opt);
            else
                System.out.println("Failure occurred, check log in WCF!");

        } catch (SOAPException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案