SOAP Web Service Tutorials - Herong's Tutorial Examples - Version 5.02, by Dr. Herong Yang
Using HttpURLConnection to Call GetSpeech SOAP 1.1
This section provides a tutorial example on how to use the java.net.HttpURLConnection class to call the GetSpeech SOAP 1.1 Web service provided by xmlme.com.
In order to use the HttpURLConnection class to call the GetSpeech Web service, we need know the structure of its request headers and SOAP message. Here is a sample of GetSpeech SOAP 1.1 Web Service provided at http://www.xmlme.com/WSShakespeare.asmx?op=GetSpeech:
POST /WSShakespeare.asmx HTTP/1.1 Host: www.xmlme.com Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://xmlme.com/WebServices/GetSpeech" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <GetSpeech xmlns="http://xmlme.com/WebServices"> <Request>string</Request> </GetSpeech> </soap:Body> </soap:Envelope>
Based on this sample, I wrote this HttpURLConnection program to call the GetSpeech SOAP 1.1 Web service:
/* HttpURLConnectionGetSpeech11.java * Copyright (c) 2009 by Dr. Herong Yang, herongyang.com * All rights reserved */ import java.io.*; import java.net.*; public class HttpURLConnectionGetSpeech11 { public static void main(String[] args) { PrintStream out = System.out; // Checking command line arguments if (args.length < 3) { out.println("Usage:"); out.println( "java HttpURLConnectionGetSpeech11 url inFile outFile"); return; } String sURL = args[0]; String inFile = args[1]; String outFile = args[2]; String action = "http://xmlme.com/WebServices/GetSpeech"; try { // Reading the SOAP request message from a file File objFile = new File(inFile); int reqLen = (int) objFile.length(); byte[] reqBytes = new byte[reqLen]; FileInputStream inStream = new FileInputStream(objFile); inStream.read(reqBytes); inStream.close(); // Creating the HttpURLConnection object URL oURL = new URL(sURL); HttpURLConnection con = (HttpURLConnection) oURL.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty( "Content-type", "text/xml; charset=utf-8"); con.setRequestProperty("SOAPAction", action); con.setDoOutput(true); con.setDoInput(true); // Posting the SOAP request XML message OutputStream reqStream = con.getOutputStream(); reqStream.write(reqBytes); reqStream.flush(); // Reading the SOAP response XML message byte[] byteBuf = new byte[1024]; FileOutputStream outStream = new FileOutputStream(outFile); InputStream resStream = con.getInputStream(); int resLen = 0; int len = resStream.read(byteBuf); while (len > -1) { resLen += len; outStream.write(byteBuf,0,len); len = resStream.read(byteBuf); } outStream.close(); reqStream.close(); resStream.close(); // Output counts out.println("Request length: "+reqLen); out.println("Response length: "+resLen); } catch (IOException e) { e.printStackTrace(); } } }
See the next tutorial for testing result of this program.
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
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
SoapUI - SOAP Web Service Testing Tool
WS-Security - SOAP Message Security Extension
WS-Security X.509 Certificate Token