itsme
itsme

Reputation: 135

how to generate soap request and get response in java coding

can anyone help me to develop a java program which will interact with the webservice.

I created a simple webservice in netbeans. it generates wsdl file and i take the url from it.

By using the wsdl file that is created in netbeans, i have to send soap request and get response in a java program.

I have the following piece of coding, but i have no idea on how to implement for my requirement

import javax.xml.soap.*;

public class SOAPClientSAAJ2 {
    public static void main(String args[]) throws Exception {
        // 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);

        // print SOAP Response
        System.out.print("Response SOAP Message:");
        soapResponse.writeTo(System.out);

        soapConnection.close();
    }

    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>[email protected]</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("[email protected]");
        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;
    }
}

Upvotes: 2

Views: 46559

Answers (1)

Rudi Angela
Rudi Angela

Reputation: 1473

I recommend using JAX-WS instead of handcrafting your SOAP message. With JAX-WS you generate the code to create the SOAP/XML and to send/receive the message/response. All you're left to do is set the values for the content you need to pass. In this case that would be creating a VerifyEmail object, setting its two attributes and calling the send method of the generated web service client:

ObjectFactory factory = new ObjectFactory;
VerifyEmailRequest request = objectFactory.createVerifyEmailRequest();
VerifyEmail msg = objectFactory.createVerifyEmail();
msg.setEmail("[email protected]");
msg.setLicenseKey("myKey");
request.setVerifyEmail(msg);
VerifyEmailResponse response = myClient.verifyEmail(reques);

All the classes mentioned here would be generated for you by JAXB, which is used by JAX-WS. You can find more detailed info here.

Upvotes: 4

Related Questions