title: how to generate soap request and get response in java coding
body: <p>can anyone help me to develop a java program which will interact with the webservice.</p>
  
  <p>I created a simple webservice in netbeans.  it generates wsdl file and i take the url from it.</p>
  
  <p>By using the wsdl file that is created in netbeans, i have to send soap request and get response in a java program.</p>
  
  <p>I have the following piece of coding, but i have no idea on how to implement for my requirement</p>
  
  <pre><code>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:
           &lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/"&gt;
           &lt;SOAP-ENV:Header/&gt;
           &lt;SOAP-ENV:Body&gt;
           &lt;example:VerifyEmail&gt;
           &lt;example:email&gt;mutantninja@gmail.com&lt;/example:email&gt;
           &lt;example:LicenseKey&gt;123&lt;/example:LicenseKey&gt;
           &lt;/example:VerifyEmail&gt;
           &lt;/SOAP-ENV:Body&gt;
           &lt;/SOAP-ENV:Envelope&gt;
           */
          // 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;
      }
  }
  </code></pre>