5

我正在尝试发送带有肥皂头的 SOAP 请求,如下所示:

<SOAP-ENV:Header>
<Security xmlns="http://www.xxx.org/xxx/2003/05">
<UsernameToken><Username>yyyy</Username><Password>xxx</Password>
</UsernameToken></Security></SOAP-ENV:Header>

为了做到这一点,我正在使用添加标题元素SoapActionCallback

SoapActionCallback actionCallBack = new SoapActionCallback("https://aaa.com/bbb.asmx") {
            public void doWithMessage(WebServiceMessage msg) {
                SoapMessage smsg = (SoapMessage) msg;
                smsg.setSoapAction("http://www.xxx.org/yyy/2003/05/SessionCreate");
                SoapHeaderElement security = smsg.getSoapHeader().addHeaderElement(new QName("http://www.xxx.org/yyy", "Security"));
                security.setText("<UsernameToken><Username>yyyy</Username><Password>xxx</Password></UsernameToken>");
            }
        };

我的问题是肥皂头看起来像这样

<SOAP-ENV:Header><Security xmlns="http://www.xxx.org/yyy/2003/05">&lt;UsernameToken&gt;&lt;Username&gt;yyyyy&lt;/Username&gt;&lt;Password&gt;xxxx&lt;/Password&gt;&lt;/UsernameToken&gt;</Security></SOAP-ENV:Header>

结果我的请求失败了:

如何正确添加此消息?

4

1 回答 1

3

我也想要同样的东西。但是,正如我自己搜索的那样,Spring 没有像 java 中的 SOAPMessage 这样的完整功能。您可能需要的是一个 addChildElement() 到标题。

我最终放弃了 SoapMessage 大楼并采用了以下方法:

SOAPMessage message = MessageFactory.newInstance();
private void buildHeader(String userName, String password) throws SOAPException{
    SOAPHeader header = message.getSOAPHeader();
    QName authHeader = new QName(SCHEMA, "AuthenticationHeader", SCHEMA_PREFIX);
    SOAPHeaderElement authElement = header.addHeaderElement(authHeader);

    QName userNameHeader = new QName(SCHEMA, "UserName", SCHEMA_PREFIX);
    SOAPElement userElement = authElement.addChildElement((userNameHeader));
    userElement.setTextContent(userName);

    QName passwordHeader = new QName(SCHEMA, "Password", SCHEMA_PREFIX);
    SOAPElement passwdElement = authElement.addChildElement(passwordHeader);
    passwdElement.setTextContent(password);


}

请注意,使用上面使用 sendSourceAndReceiveToResult 传递 SOAPMessage 的方法将 SOAPMessage 包装在另一个信封中,这不是我们想要的。:(

Update (10/25/2013): I found this solution in another discussion. It's a little vague but worth looking into. I'll try to test this out on my end. One of my colleagues tried out spring-integration-ws and looking in google for the topic and soap headers yielded some promising resources.

Update (10/25/2013): After fooling around with the library I discovered there is a quicker way to wrap the SOAPMessage into Spring's SoapMessage using this:

// message is a SOAPMessage object with custom headers
SoapMessage soapMessage = new SaajSoapMessage(message);
soapMessage.writeTo(System.out);

Perhaps after you build your soapmessage you can route it to spring-ws and so on.

于 2013-10-11T03:11:37.310 回答