Sending XML to webservice

Asked

Viewed 1,163 times

-1

I need to know how to send XML to a client’s webservice. Since I’m very new to this, I did a little research on some sources and found a very simple code for java. I wanted to know if everything is correct:

package Envio;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.OutputKeys;
//import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

public class ConsumirWebServicePorRequisicaoXML {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws SOAPException, IOException {
    // TODO code application logic here
    String requestSoap;

    requestSoap = "MEU XML";

    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection soapConnection = soapConnectionFactory.createConnection();

    String url = "MEU LINK ASMX do WEBSERVICE"; //url do webservice

    MimeHeaders headers = new MimeHeaders();
    headers.addHeader("Content-Type", "text/xml");

    MessageFactory messageFactory = MessageFactory.newInstance();

    SOAPMessage msg = messageFactory.createMessage(headers, (new ByteArrayInputStream(requestSoap.getBytes())));

    SOAPMessage soapResponse = soapConnection.call(msg, url);
    Document xmlRespostaARequisicao=soapResponse.getSOAPBody().getOwnerDocument();

    System.out.println(passarXMLParaString(xmlRespostaARequisicao,4)); //imprime na tela o xml de retorno.
}
public static String passarXMLParaString(Document xml, int espacosIdentacao){
    try {
        //set up a transformer
        TransformerFactory transfac = TransformerFactory.newInstance();
        transfac.setAttribute("indent-number", new Integer(espacosIdentacao));
        Transformer trans = transfac.newTransformer();
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");

        //create string from xml tree
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource(xml);
        trans.transform(source, result);
        String xmlString = sw.toString();
        return xmlString;
    }
    catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(0);
    }
    return null;
}
}

For obvious reasons, I omitted the XML and ASMX link from the webservice because it contains a little more sensitive information. But I have already been able to validate the XML format and I also have the webservice link.

The idea is that this application in JAVA is run once a day or once a week and send several XML to this webservice (then I change the code for it to send more than one XML).

1 answer

0

Apparently the approach is correct, basically you are creating a SOAP request, adding the parameters in the body of the request where one of them will be the XML converted into String and triggering the request.

Whether it will actually work just by testing it out, because a lot of things can vary.

What you can do to test outside of Java is to use tools like SOAPUI to test the service if there is an error in Java, because then you validate if the problem is the code, the request or the service.

  • Thank you for the answer, Vinicius. Yes, I have already seen it with the help of SOAPUI. I really just needed to know if the JAVA programming was correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.