将内容添加到soap消息(基本)

时间:2014-08-07 18:01:41

标签: java xml eclipse web-services soap

以下代码是我用来理解soap消息等概念的。但是,在这段代码中有2行表示 -

 MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI  + "VerifyEmail");

为什么打印时无法查看?另外我想添加标题“<?xml version="1.0" encoding="utf-8"?>”如何做到这一点?以及如何获得发送内容的整个输出/视图。标题和一切?谢谢。

   import javax.xml.soap.*;
    import javax.xml.transform.*;
    import javax.xml.transform.stream.*;

    public class SOAPClientSAAJ {

        /**
         * Starting point for the SAAJ - SOAP Client Testing
         */
        public static void main(String args[]) {
            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();

            String serverURI = "http://ws.cdyne.com/";

            // SOAP Envelope
            SOAPEnvelope envelope = soapPart.getEnvelope();
            envelope.addNamespaceDeclaration("example", serverURI);

            /*
            Constructed SOAP Request Message:
            <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/">
                <SOAP-ENV:Header/>
                <SOAP-ENV:Body>
                    <example:VerifyEmail>
                        <example:email>mutantninja@gmail.com</example:email>
                        <example:LicenseKey>123</example:LicenseKey>
                    </example:VerifyEmail>
                </SOAP-ENV:Body>
            </SOAP-ENV:Envelope>
             */

            // SOAP Body
            SOAPBody soapBody = envelope.getBody();
            SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
            SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example");
            soapBodyElem1.addTextNode("mutantninja@gmail.com");
            SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example");
            soapBodyElem2.addTextNode("123");

            MimeHeaders headers = soapMessage.getMimeHeaders();
            headers.addHeader("SOAPAction", serverURI  + "VerifyEmail");

            soapMessage.saveChanges();

            /* Print the request message */
            System.out.print("Request SOAP Message = ");
            soapMessage.writeTo(System.out);
            System.out.println();

            return soapMessage;
        }

        /**
         * Method used to print the SOAP Response
         */
        private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            Source sourceContent = soapResponse.getSOAPPart().getContent();
            System.out.print("\nResponse SOAP Message = ");
            StreamResult result = new StreamResult(System.out);
            transformer.transform(sourceContent, result);
        }

    }

1 个答案:

答案 0 :(得分:0)

我不确定是否有办法直接从DOM或SOAP打印它们,但您可以解析这些值并单独打印它们:

        SOAPHeader header = message.getSOAPHeader();
        if (header == null) {
            // Throw an exception
         }

        NodeList headerPartNode = header.getChildNodes();
        String headerPart = headerPartNode.item(0).getChildNodes().item(0).getNodeValue();

您可以迭代NodeList,解析每个标题部分,然后单独打印它们。