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("https://www.herongyang.com/Service");
   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 connection.

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

6. Finally read the SOAP response XML message from the connection.

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

See the next tutorial for a sample HttpURLConnection 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