First SOAPConnection Test Program

This section provides a tutorial example on how to create your first SOAPConnection test program that sends out a simple hello SOAP request message.

To see how SAAJ works, I wrote this quick test program, SOAPConnectionTest.java:

/**
 * SOAPConnectionTest.java
 * Copyright (c) 2009 by Dr. Herong Yang, herongyang.com
 * All rights reserved
 */
import java.io.*;
import javax.xml.soap.*;
public class SOAPConnectionTest {
   public static void main(String[] args) {
      PrintStream out = System.out;

// Checking command line arguments
      if (args.length < 1) {
         out.println("Usage:");
         out.println("java SOAPConnectionTest url");
         return;
      }
      String sURL = args[0];

      try {
// Building the request document
      	 SOAPMessage reqMsg 
      	    = MessageFactory.newInstance().createMessage();
         SOAPEnvelope envelope = reqMsg.getSOAPPart().getEnvelope();
         SOAPBody body = envelope.getBody();
         body.addBodyElement(envelope.createName("Hello"));

// Connecting and calling
	 SOAPConnection con 
	    = SOAPConnectionFactory.newInstance().createConnection();
         SOAPMessage resMsg = con.call(reqMsg, sURL);
         con.close();

// Showing output
         out.println("\n\nRequest:");
         reqMsg.writeTo(out);
         out.println("\n\nResponse:");
         resMsg.writeTo(out);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

To run this program, I need to run my socket request response server program with a sample SOAP 1.1 response file, soap_1_1_server.res:

HTTP/1.1 200 OK
Connection: close
Content-Type: text/xml; charset=utf-8
Content-Length: 306

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <HelloRes>Hello Herong!</HelloRes>
  </soap:Body>
</soap:Envelope>

Then run SocketRequestResponseServe.java in a command window:

\herong>java SocketRequestResponseServer soap_1_1_server.res
   server.req

Listening at 8888

Now in another command window, run SOAPConnectionTest.java:

\herong>java
   -classpath .;\herong\lib\saaj-api.jar;\herong\lib\saaj-impl.jar
   SOAPConnectionTest http://localhost:8888

Request:
<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <Hello/>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Response:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <HelloRes>Hello Herong!</HelloRes>
  </soap:Body>
</soap:Envelope>

Ok. My SOAPConnectionTest.java seems to be working by looking at the client side. Now you can check the entire request recorded by the server side test program in the first command window:

Connection received from /127.0.0.1
Request length: 422
Response length: 391

\herong\>type server.req
POST / HTTP/1.1
Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Content-Type: text/xml; charset=utf-8
Content-Length: 155
Cache-Control: no-cache
Pragma: no-cache
User-Agent: Java/1.6.0_06
Host: localhost:8888
Connection: keep-alive

<SOAP-ENV:Envelope
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <Hello/>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The request header lines generated by the SOAPConnection class all look ok me.

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

SAAJ - SOAP with Attachments API for Java

 SAAJ API 1.3 Classes and Interfaces Overview

 SAAJ API and Default Implementation in JDK 1.6.0

 SAAJ API Reference Implementation 1.3.4

First SOAPConnection Test Program

 Creating SOAPConnection and SOAPMessage Objects

 SAAJ SOAPMessage Structure and Classes/Interfaces

 Populating the SOAP Body with Request XML Elements

 Don't Use xml* as namespace Prefix

 addHeader() - Setting SOAPAction Header Line

 Calling GetSpeech SOAP 1.1 with SAAJ

 SOAPConstants.SOAP_1_2_PROTOCOL

 Calling GetSpeech SOAP 1.2 with SAAJ

 SoapUI - SOAP Web Service Testing Tool

 WS-Security - SOAP Message Security Extension

 WS-Security X.509 Certificate Token

 Web Services and SOAP Terminology

 References

 PDF Printing Version