The key for me was to turn on the SSL debugging using the VM option: "-Djavax.net.debug=ssl"
This prints out the entire SSL communication process. Most of it is just encrypted content, but at the beginning of each connection it says whether it is using a new connection or reusing the same session. The test below connects to Verisign and IBM and uses a Hashtable to store the URL objects. While this is not a robust connection pool, it illustrates the point that by reusing a URL object, the SSL session is reused.
Hope that helps.
Chris
import java.net.*;
import java.io.*;
import java.util.*;
public class SSLConnection
{
Hashtable urls = new Hashtable();
boolean debugInfo = true;
boolean SSLDebug = true;
public static void main(String args[])
{
// Need to add the Security Provider to use SSL
java.security.Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider());
SSLConnection SSL = new SSLConnection();
SSL.goToSecureSites();
}
/**
* Stores references to requested address and reuses URL objects
*/
public URLConnection getSSLConnection(String address)
throws MalformedURLException, IOException
{
URL requestAddr = null;
if (urls.containsKey(address))
{
requestAddr = (URL) urls.get(address);
}
else
{
requestAddr = new URL(address);
urls.put(address, requestAddr);
}
return requestAddr.openConnection();
}
public void goToSecureSites()
{
debug("\n\nSSLConnection.goToSecureSites(): Begin");
if (this.SSLDebug)
{
System.setProperty("javax.net.debug", "ssl");
}
try
{
debug("Connecting to Verisign");
URLConnection conn = this.getSSLConnection("https://www.verisign.com");
printContent(conn);
debug("Finished Connecting to Verisign");
System.out.print("Pausing 5 seconds:...");
Thread.currentThread().sleep(1000);
System.out.print("1...");
Thread.currentThread().sleep(1000);
System.out.print("2...");
Thread.currentThread().sleep(1000);
System.out.print("3...");
Thread.currentThread().sleep(1000);
System.out.print("4...");
Thread.currentThread().sleep(1000);
System.out.print("5...Resuming\n");
debug("Connecting to IBM");
conn = this.getSSLConnection("https://www.ibm.com");
printContent(conn);
debug("Finished Connecting to IBM");
System.out.print("Pausing 3 seconds:...");
Thread.currentThread().sleep(1000);
System.out.print("1...");
Thread.currentThread().sleep(1000);
System.out.print("2...");
Thread.currentThread().sleep(1000);
System.out.print("3...");
Thread.currentThread().sleep(1000);
System.out.print("4...");
Thread.currentThread().sleep(1000);
System.out.print("5...Resuming\n");
debug("Connecting to Verisign (again)");
conn = this.getSSLConnection("https://www.verisign.com");
printContent(conn);
debug("Finished Connecting to Verisign (again)");
System.out.print("Pausing 5 seconds:...");
Thread.currentThread().sleep(1000);
System.out.print("1...");
Thread.currentThread().sleep(1000);
System.out.print("2...");
Thread.currentThread().sleep(1000);
System.out.print("3...");
Thread.currentThread().sleep(1000);
System.out.print("4...");
Thread.currentThread().sleep(1000);
System.out.print("5...Resuming\n");
debug("Connecting to IBM (again)");
conn = this.getSSLConnection("https://www.ibm.com");
printContent(conn);
debug("Finished Connecting to IBM (again)");
}
catch(Exception e)
{
System.out.print(e);
System.exit(1);
}
}
public void printContent(URLConnection conn)
{
try
{
conn.setDoOutput(false);
conn.setDoInput(true);
System.out.println(conn.getRequestProperty("Content-Type"));
// Get response data.
BufferedReader input = new BufferedReader (
new InputStreamReader(conn.getInputStream ()));
String str = null;
while (null != ( (str = input.readLine()) ) )
{
System.out.println (str);
}
input.close ();
}
catch(Exception e)
{
System.out.println(e);
System.exit(1);
}
}
public void debug(String stmt)
{
System.out.println(new Date() + ": " + stmt);
}
}