//2012-06-15 johnpfeiffer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;
public class HttpsUtility
{
protected static String getHTTPSConnectionInfo( HttpsURLConnection connection )
{
StringBuilder strb = new StringBuilder();
String newline = System.getProperty( "line.separator" );
try
{
strb.append( "Response Code : " + connection.getResponseCode() + newline );
}catch( IOException e )
{
strb.append( "ERROR: " + e.getMessage() + newline );
}
strb.append( "Cipher Suite : " + connection.getCipherSuite() + newline );
Certificate[] certs = null;
try
{
certs = connection.getServerCertificates();
}catch( SSLPeerUnverifiedException e )
{
strb.append( "ERROR: " + e.getMessage() + newline );
}
for( Certificate cert : certs )
{
strb.append( "Cert Type : " + cert.getType() + newline );
strb.append( "Cert Hash Code : " + cert.hashCode() + newline );
strb.append( "Cert Public Key Algorithm : " + cert.getPublicKey().getAlgorithm() + newline );
strb.append( "Cert Public Key Format : " + cert.getPublicKey().getFormat() + newline );
if( cert.getType().equals( "X.509" ) )
{
try
{
byte[] encoded = cert.getEncoded();
X509Certificate x509cert = X509Certificate.getInstance( encoded );
strb.append( x509cert.getSubjectDN() );
}catch( CertificateEncodingException e )
{
strb.append( "ERROR: encoding: " + e.getMessage() );
}catch( CertificateException e )
{
strb.append( "ERROR: certificate: " + e.getMessage() );
}
}
}
return strb.toString();
}
protected static String getHTTPSConnectionContent( HttpsURLConnection connection )
{
StringBuilder strb = new StringBuilder();
// String newline = System.getProperty( "line.separator" );
BufferedReader br = null;
try{
connection.connect();
InputStream response = connection.getInputStream();
br = new BufferedReader( new InputStreamReader( response ) );
String input;
while( ( input = br.readLine() ) != null )
{
System.out.println( input );
}
br.close();
}catch( IOException ioe )
{
strb.append( ioe.toString() );
}
return strb.toString();
}
} //end class