Home Code Call SOAP with Request XML and get Response XML back

Call SOAP with Request XML and get Response XML back

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.

Disclaimer: MashTips is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission at no extra cost to you.

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 Reply

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

Free Newsletter

Read More...

5 Built-in Mac Security Software you Never Knew in OS X

Apple did an excellent job on Mac OS X Security to protect their system. Believe it or not, there are at least seven built-in Mac...

12 Best Noise Canceling Headphones for Great Music Experience.

Noise canceling headphones are coming with active noise canceling or passive noise canceling technology. When you are searching for the best noise canceling headphone, go...

How to Record iPhone Screen on Mac with Voice

Do you want to create a movie with all your screen activities on your iPhone? If you own an iPad or iPhone you can...

7 Best Android OCR Apps to Scan Image to Text

Android OCR apps can convert scanned images to text, and that is coming in handy in this digital world. The converted text will be...

How to Create A System Restore Point on Windows 10

Many Windows users over the years have complained of crashes or updates ruining something or the other on their Windows machines. Also, for many...

iCloud Storage Full: Transfer Photos from iCloud to Google Photos

Apple's iCloud has always been criticized for the lack of enough storage space, on the free plan that they offer. The company offers just...
%d bloggers like this: