Call SOAP with Request XML and get Response XML back

Last Updated: May 16, 2018
java codes

This code is a simple, general purpose SOAP client in Java that uses XML Request File. This example, we are sending an XML Request file with SOAP URL and getting back SOAP response as an 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.

Related: 10 Best Text Editors for Windows, Linux and Mac.

SOAP with Request XML

/* Call SOAP URL and send the Request XML and Get Response XML back */
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;

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);
fin.close();

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);
b=s.getBytes();

// 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”, “”);
httpConn.setRequestMethod(“POST”);
httpConn.setDoOutput(true);

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

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

while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
FileOutputStream fos=new FileOutputStream(responseFileName);
copy(httpConn.getInputStream(),fos);
in.close();
}

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

synchronized (in) {
synchronized (out) {
byte[] buffer = new byte[256];
while (true) {
int bytesRead = in.read(buffer);
if (bytesRead == -1)
break;
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 variables 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.

3 COMMENTS

    • Buenas tardes, necesito usar este ejemplo pero no se como iniciar la conexion, estoy confundido como hago esta parte, porque no se que datos debo colocar por ejemplo el PORT NUMBER Y SOAPUrl
      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();

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.