向soap消息添加XML声明

1 java xml eclipse soap soap-client

我正在尝试为 ex: 添加一个 XML 声明<?xml version="1.0" encoding="utf-8"?>到此soap消息的顶部。任何人都可以帮助我如何做到这一点?

 try {
            // Create SOAP Connection
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            // Send SOAP Message to SOAP Server
            String url = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

            // Process the SOAP Response
            printSOAPResponse(soapResponse);

            soapConnection.close();
        } catch (Exception e) {
            System.err.println("Error occurred while sending SOAP Request to Server");
            e.printStackTrace();
        }
    }

    private static SOAPMessage createSOAPRequest() throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();
        SOAPHeader header = soapMessage.getSOAPHeader();
Run Code Online (Sandbox Code Playgroud)

use*_*063 5

您需要为 SOAP 消息设置一个属性。

soapMessage.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true")
Run Code Online (Sandbox Code Playgroud)

上面的代码应该这样做。

这里是链接setProperty(String property, Object value)文档。

  • 谢谢。请注意, value 参数将在 String 中期望 true 或 false,因此发送 `Boolean,TRUE` 将产生错误。 (2认同)