Skip to content

Instantly share code, notes, and snippets.

@cpilsworth
Created March 14, 2013 09:12
Show Gist options
  • Save cpilsworth/5159973 to your computer and use it in GitHub Desktop.
Save cpilsworth/5159973 to your computer and use it in GitHub Desktop.
Simple utility to test SSL connections (i.e. certificate/trust store configuration) from java applications, use debug flag for lots of information.
package uk.co.diffa.ssltest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
public class SSLTest {
public static void main(String[] args) throws Exception {
if (args.length == 0) {
printUsage();
} else {
testConnection(args);
}
}
private static void testConnection(String[] args) throws MalformedURLException, IOException {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
URL url = new URL(args[0]);
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
conn.setSSLSocketFactory(sslsocketfactory);
InputStream inputstream = conn.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
String string = null;
while ((string = bufferedreader.readLine()) != null) {
System.out.println("Received " + string);
}
}
private static void printUsage() {
System.out.println("Usage: java -jar ssltest.jar https://{host}[/uri]");
System.out.println("-------------------------------------------------");
System.out.println("You may need some or all of the following settings:");
System.out.println("-Djavax.net.ssl.keyStoreType={pkcs12|jks}");
System.out.println("-Djavax.net.ssl.trustStoreType={pkcs12|jks}");
System.out.println("-Djavax.net.ssl.keyStore={keystore_location}");
System.out.println("-Djavax.net.ssl.trustStore={truststore_location}");
System.out.println("-Djavax.net.debug=ssl # very verbose debug");
System.out.println("-Djavax.net.ssl.keyStorePassword={password}");
System.out.println("-Djavax.net.ssl.trustStorePassword={password}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment