0

I am trying to implement a SOAP Client (i found the code here and am trying to adapt it to my problem (send a file to an endPointURL)). I have only the soapAction and the endPointURL to send my message to the server.

public class SOAPClient {

private static final Logger logger = LoggerFactory.getLogger(SOAPClient.class);
private static File file = null;

public static void main(String args[]) {

    String soapEndpointUrl = "url?wsdl";
    String soapAction = "soapAction(https)"
    file = new File("myFileToSend.xml");
    callSoapWebService(soapEndpointUrl, soapAction,file);
}

private static void createSoapEnvelope(SOAPMessage soapMessage,Document doc) throws SOAPException {


    SOAPPart soapPart = soapMessage.getSOAPPart();

    DOMSource domSource = new DOMSource(doc);
    soapPart.setContent(domSource);

    //Error here !
    SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope();

    SOAPHeader header = envelope.getHeader();



}

private static void callSoapWebService(String soapEndpointUrl, String soapAction,File f) {
    try {
        file = f;
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Send SOAP Message to SOAP Server
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);

        if(soapResponse!=null){
            // Print the SOAP Response
            logger.info("Response SOAP Message:");
            soapResponse.writeTo(System.out);
            logger.info("----------------------------------------------");

            logger.info("soap reponse : "+soapResponse);

        }
        soapConnection.close();
    } catch (Exception e) {
        logger.error("\nError occurred while sending SOAP Request to Server!\n");
        e.printStackTrace();
    }
}

private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {
    Document doc = addXmlFile();
    SOAPMessage soapMessage = null;

    if(doc!=null){

        MessageFactory messageFactory = MessageFactory.newInstance();
        soapMessage = messageFactory.createMessage();
        createSoapEnvelope(soapMessage,doc);
        soapMessage.saveChanges();

        logger.info(soapMessage.toString());

        /* Print the request message, just for debugging purposes */
        logger.info("Request SOAP Message:");
        soapMessage.writeTo(System.out);
    }

    return soapMessage;
}


@SuppressWarnings("finally")
private static Document addXmlFile() {
    Document doc = null;
    try {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(file.getAbsoluteFile());
        return doc;

    } catch (SAXException | IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

    finally {

        return doc;

    }
}

Actually when i am trying to launch this code, i got the error : SAAJ0514 unable to create envelope from given source because the root element is not named Envelope. And the error SAAJ0511 : Impossible to create an envelope from the given source

com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source: 
    at com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(EnvelopeFactory.java:117)
    at com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPPart1_1Impl.createEnvelopeFromSource(SOAPPart1_1Impl.java:69)
    at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:125)
    at SOAPClient.createSoapEnvelope(SOAPClient.java:70)
    at SOAPClient.createSOAPRequest(SOAPClient.java:132)
    at SOAPClient.callSoapWebService(SOAPClient.java:103)
    at SOAPClient.main(SOAPClient.java:39)
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source because the root element is not named "Envelope"
    at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.lookForEnvelope(SOAPPartImpl.java:154)
    at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:121)
    at com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(EnvelopeFactory.java:110)
    ... 6 more

I saw some similar error on many sites but none really helped me to find how to correct this (some explains to delete some parameters on the server).

Thank you for your possible answers.

Guru
  • 1,653
  • 1
  • 7
  • 14
Noxus
  • 3
  • 1
  • 6
  • Did you try to send a sample SOAP message to the endpoint using SOAP UI ? I suggest you try that first. – Guru Feb 14 '18 at 17:06
  • i will try it on SOAP UI ! Thanks for your advise – Noxus Feb 15 '18 at 08:16
  • Hey,I used SOAP UI and it s work. But I always have the error. I think i havn't the rigth schema: On many other code i found this uri: I don't succes to erase this uri and put the new. I try this : `envelope.addNamespaceDeclaration("soapenv","http://www.w3.org/2003/05/soap-envelope"); envelope.removeNamespaceDeclaration(envelope.getPrefix());` But it's doesn't work. Have you any idea how to process ? Thank you for your possible answers. – Noxus Feb 19 '18 at 14:14
  • 2
    I found the error : `String soapEndpointUrl = "url?wsdl";` must be `String soapEndpointUrl = "url";` – Noxus Feb 20 '18 at 15:19

1 Answers1

0

I had that error in the company I work with and removing ?wsdl part was the exact thing I needed to do for it to work.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
xoan
  • 1
  • 1