Here’s my crude test client that reads a soap message from a file in the file system and posts it to the server of your choice. It will accomodate only HTTP and not HTTPS connections.
package conneva.soap.utils;
import java.net.*;
import java.net.URLEncoder;
import java.io.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import sun.misc.BASE64Encoder;
/**
*
* This is a simple text mode application that reads a properly formed SOAP message (including the envelope) from an input file
* and sends it to the specified URL. Authorization information can be provided from the command line.
*
* Basic performance metrics are provided to measure elapsed time to send and receive the SOAP messages.
*
*/
public class SendSoapMsgHTTP {
public final static String DEFAULT_SERVER
= "http://localhost:8080/soap/default";
public final static String SOAP_ACTION
= "";
public final static String DEFAULT_USERNAME = "esb_user";
public final static String DEFAULT_PASSWORD = "esb_password";
public static void main(String[] args) {
if (args.length == 0) {
if ((args.length != 1) && (args.length != 2) && (args.length != 4) && (args.length != 5)) {
System.out.println();
System.out.println("Usage: java SendSoapMsg <filename> [<url>] [<username> <password>]");
System.out.println("<filename> - valid Soap Message file to be posted");
System.out.println("<url> - valid URL (e.g. http://localhost:8080/soap/default");
System.out.println("<username> - username");
System.out.println("<password> - password");
System.out.println("<printmsgs> - 'true' displays the soap request and response msgs");
System.out.println();
return;
}
}
String fileName = args[0];
String server = DEFAULT_SERVER;
String userName = DEFAULT_USERNAME;
String password = DEFAULT_PASSWORD;
if (args.length >= 2) server = args[1];
if (args.length >2) {
userName = args[2];
password = args[3];
}
String verboseStr = "";
boolean verbose = false;
if (args.length == 5) {
verboseStr = args[4];
if (verboseStr.equals("true")) verbose = true;
}
try {
//Get soap message from input file
File inputFile = new File(fileName);
FileInputStream in = new FileInputStream(inputFile);
byte bt[] = new byte[(int)inputFile.length()];
in.read(bt);
String soapMsg = new String(bt);
in.close();
//Get the HTTP connection setup start time
Calendar cal_csu = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
String csu_starttime = sdf.format(cal_csu.getTime());
long csu_startmillis = System.currentTimeMillis();
//Create connection to the server
URL u = new URL(server);
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
//Set connection parms
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
//Set connection authorization
String userInfo = userName + ":" + password;
BASE64Encoder encoder = new BASE64Encoder();
byte[] userInfoBytes = userInfo.getBytes(); // I18n bug here!
String authInfo = "Basic " + encoder.encode(userInfoBytes);
connection.setRequestProperty("Authorization", authInfo);
System.out.println("\nHTTP connection established. Sending soap request...\n");
//Get an output stream on the connection and create a writer
OutputStream out = connection.getOutputStream();
Writer wout = new OutputStreamWriter(out);
// Print the soap request message to the console
if (verbose) System.out.println("SOAP Request Msg: "+soapMsg+"\n");
System.out.println("SOAP request message sent. Waiting for response...\n");
//Get the start time
Calendar cal = Calendar.getInstance();
String startTime = sdf.format(cal.getTime());
long startmillis = System.currentTimeMillis();
//Send the soap message using the HTTP connection
wout.write(soapMsg);
wout.flush();
wout.close();
//Get an inputstream and read the soap response
InputStream instream = connection.getInputStream();
int c;
while ((c = instream.read()) != -1) if (verbose) System.out.write(c);
instream.close();
//Get send soap msg stop time and calculate elapsed time
Calendar cal2 = Calendar.getInstance();
String endTime = sdf.format(cal2.getTime());
long endmillis = System.currentTimeMillis();
long csu_elapsedTime = (startmillis - csu_startmillis);
long elapsedTime = (endmillis - startmillis);
//Print performance summary
System.out.println("\nHTTP setup start : "+csu_starttime);
System.out.println("HTTP setup ms : "+ csu_elapsedTime+"\n");
System.out.println("SoapRequest sent : "+startTime);
System.out.println("SoapResponse rcvd : "+endTime);
System.out.println("Elapsed time ms : "+ elapsedTime);
}
//catch exceptions
catch (IOException e) {
System.err.println(e);
}
} // end main
} // end SendSoapMsg
#Flow-and-Java-services#webMethods#Integration-Server-and-ESB