addHeader() - Setting SOAPAction Header Line

This section provides a tutorial example on how to use the MimeHeaders class and the addHeader() method to add the SOAPAction header line for the final HTTP request.

From previous tutorials, we learned enough to generated a working SOAP XML message with simple body element under specific namespace.

One thing left to make a SOAP 1.1 HTTP post is the required SOAPAction header line, which can be generated by using these methods.

1. Assuming "request" is the new SOAPMessage object, we need to get hold of the MimeHeaders object with the getMimeHeaders() method:

   MimeHeaders headers =msg.getMimeHeaders();

2. Then we need to add the SOAPAction header line with the addHeader() method:

   headers.addHeader("SOAPAction", action);

Here is my testing program for adding SOAPAction header line:

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

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

      try {
// Creating a new empty SOAP message object
         SOAPMessage reqMsg
            = MessageFactory.newInstance().createMessage();

// Populating SOAP body
         String prefix = "tns";
         String uri = "";
         SOAPEnvelope envelope = reqMsg.getSOAPPart().getEnvelope();
         SOAPBody body = envelope.getBody();
         SOAPBodyElement service = body.addBodyElement(
            envelope.createName("GetSpeech", prefix, uri));
         SOAPElement param = service.addChildElement(
            envelope.createName("Request", prefix, uri));
         param.addTextNode("To be, or not to be");

// Setting SOAPAction header line
         MimeHeaders headers = reqMsg.getMimeHeaders();
         headers.addHeader("SOAPAction", uri+"/GetSpeech");

// Connecting and calling
	 SOAPConnection con 
	    = SOAPConnectionFactory.newInstance().createConnection();
         SOAPMessage resMsg =, sURL);
         if (resMsg != null) out.println("Response received!");
      } catch (Exception e) {

Remember that I need to run my socket server test program first to capture the entire HTTP request:

\herong>java SocketRequestResponseServer soap_1_1_server.res

Listening at 8888

Now run in another command window:

\herong>java SOAPMessageGetMimeHeaders http://localhost:8888
Response received!

Go back to the first command window:

\herong>java SocketRequestResponseServer soap_1_1_server.res

Listening at 8888
Connection received from /
Request length: 584
Response length: 391

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

    <tns:GetSpeech xmlns:tns="">
      <tns:Request>To be, or not to be</tns:Request>

The SOAPAction header line is correctly generated in the final HTTP request.

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


 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


 PDF Printing Version