Using HttpURLConnection to Call SOAP 1.1

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

Here is my generic HttpURLConnection program to call any SOAP 1.1 Web service:

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

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

      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", "text/xml; charset=utf-8");
         con.setRequestProperty("SOAPAction", action);
         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();
      }
   }
}

See the next tutorial for testing result of this program.

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

 Python SOAP Client: Zeep

 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

 Archived Tutorials

 References

 Full Version in PDF/EPUB