Hello.

Trying to write the simplest Java client to connect to the SOAP web service.

Checked option with SOAP SAAJ. It works, but it does not suit the fact that it is necessary to compose its own SOAP object model for each SOAP request.

Are there other similar options for forming a SOAP request that are completely identical to the type of SOAP SAAJ.

I tried to replace it

         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:
        
            
            
                
                     mutantninja@gmail.com
                     123
                
            
        
          * /

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

replaced by

MessageFactory msgFactory = MessageFactory.newInstance(); String reqEnv = "<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>"; SOAPMessage soapMessage = msgFactory.createMessage(null, new ByteArrayInputStream(reqEnv.getBytes())); 

and

  SOAPMessage message = MessageFactory.newInstance().createMessage(); SOAPPart soapPart = message.getSOAPPart(); soapPart.setContent(new StreamSource(new FileInputStream("c:\\musor\\1.xml"))); 

despite the fact that the resulting SOAP request is the same everywhere I get an error in the remaining 2 cases

 <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <soap:Fault> <faultcode>soap:Client</faultcode> <faultstring>Server did not recognize the value of HTTP Header SOAPAction: .</faultstring> <detail /> </soap:Fault> </soap:Body> </soap:Envelope> 

BUT only the object first method works on SAAJ.

Are there other similar methods? if so what.

Thank you in advance for your response.

    1 answer 1

    In general, I found a solution.

      public static void main(String args[]) throws Exception { String addr = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx"; String request = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:example=\"http://ws.cdyne.com/\"><SOAP-ENV:Header/><SOAP-ENV:Body>\n" + "<example:VerifyEmail><example:email>mutantninja@gmail.com</example:email>\n" + "<example:LicenseKey>123</example:LicenseKey></example:VerifyEmail>\n" + "</SOAP-ENV:Body></SOAP-ENV:Envelope>"; URL url = null; try { url = new URL(addr); } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } HttpURLConnection connection = null; try { connection = (HttpURLConnection) url.openConnection(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } connection.setRequestProperty("Content-Length", String.valueOf(request.length())); connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("SoapAction", "http://ws.cdyne.com/VerifyEmail"); connection.setDoOutput(true); PrintWriter pw = null; try { pw = new PrintWriter(connection.getOutputStream()); } catch (IOException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } pw.write(request); pw.flush(); try { connection.connect(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } BufferedReader rd = null; try { rd = new BufferedReader(new InputStreamReader( connection.getInputStream())); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String line; String respond = ""; try { respond = rd.readLine(); while ((line = rd.readLine()) != null) respond = line; } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } System.out.println(respond); }