Call SOAP with Request XML and get Response XML back

Last Updated: April 24, 2017
java codes

This code is a simple, general purpose SOAP client in Java that uses XML Request File. This examples, we are sending a XML Request file with SOAP URL and getting back SOAP response as XML file.

The Java code, it is  opening up an HTTP connection, connecting through the proxy (you can comment it out if you are not using any proxy in your environment), sending the appropriate XML to invoke a remote method, and then reading the XML response returned by the server.

SOAP with Request XML

/* Call SOAP URL and send the Request XML and Get Response XML back */

public class SoapXML {

    public static void sendSoapRequest(String cisId) throws Exception {
//use this if you need proxy to connect
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("YOUR PROXY", PORT NUMBER));
        String SOAPUrl = "WISDL URL HERE";
        String xmlFile2Send = ".\src\request.xml";
        String responseFileName = ".\src\response.xml";

        // Create the connection with http
        URL url = new URL(SOAPUrl);
        URLConnection connection = url.openConnection(proxy);
        HttpURLConnection httpConn = (HttpURLConnection) connection;
        FileInputStream fin = new FileInputStream(xmlFile2Send);
        ByteArrayOutputStream bout = new ByteArrayOutputStream();

        copy(fin, bout);

        byte[] b = bout.toByteArray();
        StringBuffer buf=new StringBuffer();
        String s=new String(b);

//replacing a sample value in Request XML
        s=s.replaceAll("VALUE", value);

        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("SOAPAction", "");

        // send the XML that was read in to b.
        OutputStream out = httpConn.getOutputStream();

        // Read the response.
        System.out.println("http connection status :"+ httpConn.getResponseMessage());
        InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
        BufferedReader in = new BufferedReader(isr);

        while ((inputLine = in.readLine()) != null)
        FileOutputStream fos=new FileOutputStream(responseFileName);

    public static void copy(InputStream in, OutputStream out)
            throws IOException {

        synchronized (in) {
            synchronized (out) {
                byte[] buffer = new byte[256];
                while (true) {
                    int bytesRead =;
                    if (bytesRead == -1)
                    out.write(buffer, 0, bytesRead);

In this example we are modifying a value of the default request XML, you can use this snippet to modify more varaibles in XML or you can comment it out. Please use SOAP UI client with the URL and request XML file to make sure it is working with your URL and XML file and getting back a XML response.


Leave a comment