SOAP Web Service Tutorials - Herong's Tutorial Examples - v5.11, by Dr. Herong Yang
SocketRequestResponse.java - Socket Client Testing Program
This section provides a tutorial example on how to write a Java program using socket interface to send a request and receive a response in binary mode.
Since most Web services are based on the TCP socket communication, it is very important to have a socket level client testing program ready to troubleshoot your Web service applications.
So I wrote this simple socket program, SocketRequestResponse.java, that can be used to send a request message and receive the response message to a remote TCP based server.
/* SocketRequestResponse.java * Copyright (c) HerongYang.com. All Rights Reserved. * All rights reserved */ import java.io.*; import java.net.*; public class SocketRequestResponse { public static void main(String[] args) { PrintStream out = System.out; // Checking command line arguments if (args.length < 4) { out.println("Usage:"); out.println( "java SocketRequestResponse host port in_file out_file"); return; } String host = args[0]; int port = (Integer.valueOf(args[1])).intValue(); String inFile = args[2]; String outFile = args[3]; try { // Reading the request 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(); // Sending request out.println("Connecting to "+host+" at "+port); Socket con = new Socket(host,port); OutputStream reqStream = con.getOutputStream(); reqStream.write(reqBytes); reqStream.flush(); // Reading response 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); out.println("Bytes received: "+len); len = resStream.read(byteBuf); } reqStream.close(); resStream.close(); outStream.close(); // Output counts out.println("Request length: "+reqLen); out.println("Response length: "+resLen); } catch (Exception e) { e.printStackTrace(); } } }
Note that:
I tested this program with several standard communication protocols like HTTP 1.0, HTTP 1.1, SOAP 1.1 and SOAP 1.2. My program worked fine in all cases. If you are interested in request and response examples of those protocols, see related Perl tutorials in this book.
Table of Contents
Introduction to SOAP (Simple Object Access Protocol)
SOAP Message Transmission and Processing
SOAP MEP (Message Exchange Patterns)
PHP SOAP Extension Client Programs
PHP SOAP Extension Server Programs
PHP SOAP Web Service Example - getTemp
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
►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