SOAP Web Service Tutorials - Herong's Tutorial Examples - Version 5.02, by Dr. Herong Yang
Calling GetSpeech SOAP 1.1 with SAAJ
This section provides a tutorial example on how to call the GetSpeech SOAP 1.1 Web service provided at xmlme.com with the SOAPConnection class defined in SAAJ.
After learned how to populate the SOAP message body and set the SOAPAction header line, I wrote this SAAJ program call the GetSpeech SOAP 1.1 Web service at www.xmlme.com:
/** * SOAPConnectionGetSpeech11.java * Copyright (c) 2009 by Dr. Herong Yang, herongyang.com * All rights reserved */ import java.io.*; import javax.xml.soap.*; import javax.xml.soap.Name; import javax.xml.namespace.QName; public class SOAPConnectionGetSpeech11 { public static void main(String[] args) { PrintStream out = System.out; // Checking command line arguments if (args.length < 1) { out.println("Usage:"); out.println("java SOAPConnectionGetSpeech11 phrase"); return; } String phrase = args[0]; String sURL = "http://www.xmlme.com/WSShakespeare.asmx"; try { // Creating a new empty SOAP message object SOAPMessage reqMsg = MessageFactory.newInstance().createMessage(); // Populating SOAP body String prefix = "tns"; String uri = "http://xmlme.com/WebServices"; 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(phrase); // Setting SOAPAction header line MimeHeaders headers = reqMsg.getMimeHeaders(); headers.addHeader("SOAPAction", uri+"/GetSpeech"); // Connecting and calling SOAPConnection con = SOAPConnectionFactory.newInstance().createConnection(); SOAPMessage resMsg = con.call(reqMsg, sURL); con.close(); // Parsing the response body = resMsg.getSOAPBody(); out.println("GetSpeechResult:"); out.println(body.getFirstChild().getFirstChild() .getTextContent()); } catch (Exception e) { e.printStackTrace(); } } }
Ready to try this program? Here is my first test result:
\herong>java SOAPConnectionGetSpeech11 "To be, or not to be" GetSpeechResult: <SPEECH><PLAY>HAMLET</PLAY><SPEAKER>HAMLET</SPEAKER>To be, or not to be: that is the question: Whether 'tis nobler in the mind to suffer ...
Congratulations!
Last update: 2009.
Table of Contents
Introduction to SOAP (Simple Object Access Protocol)
SOAP Message Transmission and Processing
SOAP Message Exchange Patterns
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