将XML声明添加到soap消息中

时间:2014-08-07 19:12:15

标签: java xml eclipse soap soap-client

我正在尝试将ex:<?xml version="1.0" encoding="utf-8"?>的XML声明添加到此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();

2 个答案:

答案 0 :(得分:2)

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

soapMessage.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true")

上面的代码应该这样做。

以下是linksetProperty(String property, Object value)文档。

答案 1 :(得分:1)

soapMessage.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");
soapMessage.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");