Press enter to see results or esc to cancel.


JAVA – Send SOAP XML Request and Read Response


In the video I have shown how you can create an XML request and send to an endpoint URL, Then receive the response from the server. You can send both get request and post request with below examples.

Send_XML_Post_Request_1.java and Send_XML_Post_Request_2.java are 2 different example codes.

Send_XML_Post_Request_1.java

package com.chillyfacts.com;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Send_XML_Post_Request_1 {
 public static void main(String[] args) {
 try {
 String url = "http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx?op=GetHolidaysAvailable";
 URL obj = new URL(url);
 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
 con.setRequestMethod("POST");
 con.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");
 String countryCode="Canada";
 String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
 "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"> " +
 " <soap12:Body> " +
 " <GetHolidaysAvailable xmlns=\"http://www.holidaywebservice.com/HolidayService_v2/\"> " +
 " <countryCode>"+countryCode+"</countryCode>" +
 " </GetHolidaysAvailable>" +
 " </soap12:Body>" +
 "</soap12:Envelope>";
 con.setDoOutput(true);
 DataOutputStream wr = new DataOutputStream(con.getOutputStream());
 wr.writeBytes(xml);
 wr.flush();
 wr.close();
 String responseStatus = con.getResponseMessage();
 System.out.println(responseStatus);
 BufferedReader in = new BufferedReader(new InputStreamReader(
 con.getInputStream()));
 String inputLine;
 StringBuffer response = new StringBuffer();
 while ((inputLine = in.readLine()) != null) {
 response.append(inputLine);
 }
 in.close();
 System.out.println("response:" + response.toString());
 } catch (Exception e) {
 System.out.println(e);
 }
 }
}

Output on Eclipse console

OK
response:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetHolidaysAvailableResponse xmlns="http://www.holidaywebservice.com/HolidayService_v2/"><GetHolidaysAvailableResult><HolidayCode><Code>NEW-YEARS-DAY-ACTUAL</Code><Description>New Year's Day</Description></HolidayCode><HolidayCode><Code>NEW-YEARS-DAY-OBSERVED</Code><Description>New Year's Day</Description></HolidayCode><HolidayCode><Code>DAY-AFTER-NEW-YEARS-DAY</Code><Description>Day After New Year's Day (QC)</Description></HolidayCode><HolidayCode><Code>EPIPHANY</Code><Description>Epiphany</Description></HolidayCode><HolidayCode><Code>QUEBEC-FLAG-DAY</Code><Description>Quebec Flag Day</Description></HolidayCode><HolidayCode><Code>GROUNDHOG-DAY</Code><Description>Groundhog Day</Description></HolidayCode><HolidayCode><Code>VALENTINES-DAY</Code><Description>Valentine's Day</Description></HolidayCode></GetHolidaysAvailableResult></GetHolidaysAvailableResponse></soap:Body></soap:Envelope>

Send_XML_Post_Request_2.java

package com.chillyfacts.com;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Send_XML_Post_Request_2 {
 public static void main(String[] args) {
 try {
 String url = "http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx?op=GetCountriesAvailable";
 URL obj = new URL(url);
 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
 con.setRequestMethod("POST");
 con.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");
 String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?> " +
 "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\"> " +
 " <soap12:Body><GetCountriesAvailable xmlns=\"http://www.holidaywebservice.com/HolidayService_v2/\" />" +
 " </soap12:Body> " +
 "</soap12:Envelope>";
 con.setDoOutput(true);
 DataOutputStream wr = new DataOutputStream(con.getOutputStream());
 wr.writeBytes(xml);
 wr.flush();
 wr.close();
 String responseStatus = con.getResponseMessage();
 System.out.println(responseStatus);
 BufferedReader in = new BufferedReader(new InputStreamReader(
 con.getInputStream()));
 String inputLine;
 StringBuffer response = new StringBuffer();
 while ((inputLine = in.readLine()) != null) {
 response.append(inputLine);
 }
 in.close();
 System.out.println("response:" + response.toString());
 } catch (Exception e) {
 System.out.println(e);
 }
 }
}

Output on Eclipse console,

OK
response:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetCountriesAvailableResponse xmlns="http://www.holidaywebservice.com/HolidayService_v2/"><GetCountriesAvailableResult><CountryCode><Code>Canada</Code><Description>Canada</Description></CountryCode><CountryCode><Code>GreatBritain</Code><Description>Great Britain and Wales</Description></CountryCode><CountryCode><Code>IrelandNorthern</Code><Description>Northern Ireland</Description></CountryCode><CountryCode><Code>IrelandRepublicOf</Code><Description>Republic of Ireland</Description></CountryCode><CountryCode><Code>Scotland</Code><Description>Scotland</Description></CountryCode><CountryCode><Code>UnitedStates</Code><Description>United States</Description></CountryCode></GetCountriesAvailableResult></GetCountriesAvailableResponse></soap:Body></soap:Envelope>