Using HttpURLConnection to Call SOAP 1.2

This section provides a tutorial example on how to use the java.net.HttpURLConnection class to call SOAP 1.2 Web services.

dataaccess.com also provides NumberToWords Web Service in SOAP 1.2 format. In order to test this, I wrote this HttpURLConnection program to call the NumberToWords SOAP 1.2 Web service:

/* HttpURLConnection12.java
 * Copyright (c) 2009-2018 HerongYang.com, All Rights Reserved.
 */
import java.io.*;
import java.net.*;
public class HttpURLConnection12 {
   public static void main(String[] args) {
      PrintStream out = System.out;

// Checking command line arguments
      if (args.length < 3) {
         out.println("Usage:");
         out.println(
            "java HttpURLConnection12 url inFile outFile");
         return;
      }
      String sURL = args[0];
      String inFile = args[1];
      String outFile = args[2];

      try {
// Reading the SOAP request message from a file
         File objFile = new File(inFile);
         int reqLen = (int) objFile.length();
         byte[] reqBytes = new byte[reqLen];
         FileInputStream inStream = new FileInputStream(objFile);
         inStream.read(reqBytes);
         inStream.close();

// Creating the HttpURLConnection object
         URL oURL = new URL(sURL);
         HttpURLConnection con 
            = (HttpURLConnection) oURL.openConnection();
         con.setRequestMethod("POST");
         con.setRequestProperty(
            "Content-type", "application/soap+xml; charset=utf-8");
         con.setDoOutput(true);
         con.setDoInput(true);

// Posting the SOAP request XML message
         OutputStream reqStream = con.getOutputStream();
         reqStream.write(reqBytes);
         reqStream.flush();

// Reading the SOAP response XML message
         byte[] byteBuf = new byte[1024];
         FileOutputStream outStream = new FileOutputStream(outFile);
         InputStream resStream = con.getInputStream();
         int resLen = 0;
         int len = resStream.read(byteBuf);
         while (len > -1) {
            resLen += len;
            outStream.write(byteBuf,0,len);
            len = resStream.read(byteBuf);
         }
         outStream.close();

         reqStream.close();
         resStream.close();

// Output counts
         out.println("Request length: "+reqLen);
         out.println("Response length: "+resLen);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

This program is identical to HttpURLConnection11.java, except the "Content-Type" and "SOAPAction" header lines.

See the next tutorial for test result of this program.

Last update: 2018.

Table of Contents

 About This Book

 Introduction to Web Service

 Introduction to SOAP (Simple Object Access Protocol)

 SOAP Message Structure

 SOAP Message Transmission and Processing

 SOAP Data Model

 SOAP Encoding

 SOAP RPC Presentation

 SOAP Properties Model

 SOAP MEP (Message Exchange Patterns)

 SOAP HTTP Binding

 SOAP PHP Implementations

 PHP SOAP Extension Client Programs

 PHP SOAP Extension Server Programs

 PHP SOAP Web Service Example - getTemp

 SOAP Perl Implementations

 Perl SOAP::Lite - SOAP Server-Client Communication Module

 Perl Socket Test Program for HTTP and SOAP

 Perl SOAP::Lite for NumberToWords SOAP 1.1 Web Service

 Perl SOAP::Lite for SOAP 1.2 Web Services

 Perl SOAP::Lite for WSDL

 SOAP Java Implementations

Java Socket and HttpURLConnection for SOAP

 SocketRequestResponse.java - Socket Client Testing Program

 SocketRequestResponseServer.java - Socket Server Testing Program

 Capturing the HTTP Request from a Browser

 "read(byteBuf) = -1" Not Working

 Using java.net.HttpURLConnection to Send SOAP Messages

 Using HttpURLConnection to Call SOAP 1.1

 Capturing HTTP Request Generated by the HttpURLConnection Class

 Calling NumberToWords SOAP 1.1 Web Service

Using HttpURLConnection to Call SOAP 1.2

 Calling NumberToWords SOAP 1.2 Web Service

 SAAJ - SOAP with Attachments API for Java

 SoapUI - SOAP Web Service Testing Tool

 WS-Security - SOAP Message Security Extension

 WS-Security X.509 Certificate Token

 Perl SOAP::Lite for GetSpeech SOAP 1.1 Web Service

 Perl SOAP::Lite 0.710 for SOAP 1.2 Web Services

 Perl SOAP::Lite 0.710 for WSDL

 Web Services and SOAP Terminology

 Outdated Tutorials

 References

 Full Version in PDF/EPUB