Herong's Tutorial Notes on Web Service and SOAP
Dr. Herong Yang, Version 4.00

Using java.net.HttpURLConnection to Send SOAP Messages

This section describes steps to follow if you want to use the java.net.HttpURLConnection class to send out a SOAP XML message.

With both client side and server side testing programs ready to capture both request and response, I am ready to try to call Web services with the HttpURLConnection class in the java.net package.

The HttpURLConnection class is a higher level communication tool comparing to the Socket class. It is relatively easy to use for calling a Web service:

1. The first step is to create HttpURLConnection object with the end point URL. Remember to use the openConnection() method on a URL object create a HttpURLConnection object, because HttpURLConnection offers no constructors.

   URL oURL = new URL("http://www.herongyang.com/MyService");
   HttpURLConnection con = (HttpURLConnection) oURL.openConnection();

2. Use the setRequestMethod() method to set the HTTP POST command:

   con.setRequestMethod("POST");

3. Use the setRequestProperty() method to set header lines:

   con.setRequestProperty("Content-type", "text/xml; charset=utf-8");
   con.setRequestProperty("SOAPAction", 
      "http://herongyang.com/MyService#MyMethod");

4. Prepare the entire SOAP request XML message as a string by using a DOM object, reading from a file, or simply as:

   reqXML = "<?xml ...<soap:Envelope ...";

5. Then write the XML message to the connetion. reading from a file, or simply as:

   OutputStream reqStream = con.getOutputStream();
   reqStream.write(reqXML.getBytes());

6. Finally read the SOAP response XML message from the connection reading from a file, or simply as:

   InputStream resStream = con.getInputStream();
   byte[] byteBuf = new byte[10240];
   int len = resStream.read(byteBuf);

See the next tutorial for a sample HttpURLConnection program.

Last update: 2009.

Table of Contents

 About This Book

 Web Services and SOAP Terminology

 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

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

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2009
Using java.net.HttpURLConnection to Send SOAP Messages