Saturday 30 April 2016

How to construct the XML document by using java code to send the SOAP request




Consider below Web Service URL and XML document.


Web Service URL = http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx


<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>



Code for construct the XML document by using java code to send the SOAP request


 MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
 SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();

envelope.addNamespaceDeclaration("example", "http://ws.cdyne.com/");

SOAPBody soapBody = envelope.getBody();


SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example").addTextNode("mutantninja@gmail.com");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example").addTextNode("123");


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



By using above code we can construct the XML document by using java code to send the SOAP request message.


No comments:

Post a Comment