向soap消息添加XML声明



我试图在此soap消息的顶部添加ex: <?xml version="1.0" encoding="utf-8"?>的XML声明。有人能告诉我怎么做吗?

 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();

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

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

上面的代码应该做到这一点。

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

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

相关内容

  • 没有找到相关文章

最新更新