java - 未调用 SOAPMessage.writeTo 时 SOAP 服务调用失败

标签 java web-services soap axis

我正在尝试调用一个 SOAP 服务,但是在我构建了我的 SOAPMessage 之后,如果我调用 SOAPMessage.writeTo(out)服务调用已成功完成,但当我省略它时它会失败。

我很确定在发送请求之前调用 writeTo() 不是强制性步骤,我做错了什么。

有什么想法吗?

这里是详细信息

我的客户

public class Test {
    public static void main(String args[]) throws Exception {
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Send SOAP Message to SOAP Server
        String url = "https://mydomain.com/webservices/gateway/IdMgt/CorporateDirectoryLookupPort";
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

        // print SOAP Response
        soapResponse.writeTo(System.out);
        soapConnection.close();
    }

    private static SOAPMessage createSOAPRequest() throws Exception {

        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
                + "<soapenv:Header>"
                + "<ns1:Security soapenv:mustUnderstand=\"0\" xmlns:ns1=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
                + "<ns1:UsernameToken>"
                + "<ns1:Username></ns1:Username>"
                + "<ns1:Password></ns1:Password>"
                + "</ns1:UsernameToken>"
                + "</ns1:Security>"
                + "</soapenv:Header>"
                + "<soapenv:Body>"
                + "<GetAccountDetailsRequest2 xmlns=\"http://anotherdomain/schema/tCorporateDirectoryLookupV1\">"
                + "<MessageHeader xmlns=\"\"/><UserID xmlns=\"\"></UserID>"
                + "<AccountID xmlns=\"\">ServiceDeskAPIprd</AccountID>"
                + "</GetAccountDetailsRequest2></soapenv:Body>"
                + "</soapenv:Envelope>";

        InputStream is = new ByteArrayInputStream(xml.getBytes());
        SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(
                null, is);

        /* Print the request message */
        soapMessage.writeTo(System.out);
        System.out.println();
        return soapMessage;
    }
}

createSOAPRequest 中的 soapMessage.writeTo(System.out)NOT 注释掉时,我得到了一个有效的响应,但是当它被注释掉时我明白了

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
  <soapenv:Fault>
   <faultcode xmlns:ns1="http://xml.apache.org/axis/">ns1:Client.NoSOAPAction</faultcode>
   <faultstring>no SOAPAction header!</faultstring>
   <detail>
    <ns2:hostname xmlns:ns2="http://xml.apache.org/axis/">lxvirt150</ns2:hostname>
   </detail>
  </soapenv:Fault>
 </soapenv:Body>
</soapenv:Envelope>

最佳答案

通过查看 writeTo 方法的实现,我发现他们在其末尾设置了“SOAPAction” header :

if (isCorrectSoapVersion(4)) {
        String[] soapAction = this.headers.getHeader("SOAPAction");

        if ((soapAction == null) || (soapAction.length == 0)) {
            this.headers.setHeader("SOAPAction", "\"\"");
    }
}

因此,如果您想避免调用 writeTo 方法,您实际上可以在创建 SOAPMessage 后立即自行设置 header :

SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(
            null, is);
soapMessage.getMimeHeaders().addHeader("SOAPAction", "\"\"");

这对我有用。希望对您有所帮助!

皮埃尔

关于java - 未调用 SOAPMessage.writeTo 时 SOAP 服务调用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27865079/

相关文章:

java - 从 soap header 中删除 mustUnderstand 属性

java - Android 地磁场不工作

java - 如何用一个循环遍历二维数组?

Java Axis2 发生意外错误。 407 需要代理验证

c# - 如何配置 WebService 返回 ArrayList 而不是 Array?

java - Spring 3.0 JAX-WS 和/或与 Apache CXF 对比

c# - 如何阻止 Spyne 将参数包装在复杂类型中?

xml - 如何从浏览器发布 SOAP 请求?

java - 消除 Maven POM 冗余

asp.net-mvc-4 - 使用 servicestack 使用 wsdl SOAPS 服务