Using HttpURLConnection to Call SOAP 1.1

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

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

 * Copyright (c) 2002 All Rights Reserved.
public class HttpURLConnection11 {
   public static void main(String[] args) {
      PrintStream out = System.out;

// Checking command line arguments
      if (args.length < 4) {
            "java HttpURLConnection11 url action inFile outFile");
      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);;

// Creating the HttpURLConnection object
         URL oURL = new URL(sURL);
         HttpURLConnection con 
            = (HttpURLConnection) oURL.openConnection();
            "Content-type", "text/xml; charset=utf-8");
         con.setRequestProperty("SOAPAction", action);

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

// 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 =;
         while (len > -1) {
            resLen += len;
            len =;


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

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 - Socket Client Testing Program - Socket Server Testing Program

 Capturing the HTTP Request from a Browser

 "read(byteBuf) = -1" Not Working

 Using 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


 Full Version in PDF/EPUB