Using HttpURLConnection to Call GetSpeech SOAP 1.1

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

In order to use the HttpURLConnection class to call the GetSpeech Web service, we need know the structure of its request headers and SOAP message. Here is a sample of GetSpeech SOAP 1.1 Web Service provided at

POST /WSShakespeare.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: ""

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi=""
    <GetSpeech xmlns="">

Based on this sample, I wrote this HttpURLConnection program to call the GetSpeech SOAP 1.1 Web service:

 * Copyright (c) 2009 by Dr. Herong Yang,
 * All rights reserved
public class HttpURLConnectionGetSpeech11 {
   public static void main(String[] args) {
      PrintStream out = System.out;

// Checking command line arguments
      if (args.length < 3) {
            "java HttpURLConnectionGetSpeech11 url inFile outFile");
      String sURL = args[0];
      String inFile = args[1];
      String outFile = args[2];

      String action = "";

      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.

Last update: 2009.

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 Message Exchange Patterns

 SOAP HTTP Binding

 SOAP Perl Implementations

 SOAP PHP Implementations

 SOAP Java Implementations

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

 Perl Socket Test Program for HTTP and SOAP

 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

 PHP SOAP Extension Client Programs

 PHP SOAP Extension Server Programs

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 GetSpeech SOAP 1.1

 Capturing HTTP Request Generated by the HttpURLConnection Class

 Calling GetSpeech SOAP 1.1 Web Service

 Using HttpURLConnection to Call GetSpeech SOAP 1.2

 Calling GetSpeech 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

 Web Services and SOAP Terminology


 PDF Printing Version