Created
August 19, 2015 00:04
-
-
Save emersonbarros/58ed887665e614a46b32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package foo; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
import java.security.cert.Certificate; | |
import javax.net.ssl.HttpsURLConnection; | |
import javax.net.ssl.SSLPeerUnverifiedException; | |
public class JavaHttpsExample { | |
public static void main(String[] args) throws Exception { | |
String httpsURL = "https://www.google.com/"; | |
URL myurl = new URL(httpsURL); | |
HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection(); | |
printHttpsCert(con); | |
InputStream ins = con.getInputStream(); | |
InputStreamReader isr = new InputStreamReader(ins); | |
BufferedReader in = new BufferedReader(isr); | |
String inputLine; | |
while ((inputLine = in.readLine()) != null) { | |
System.out.println(inputLine); | |
} | |
in.close(); | |
} | |
private static void printHttpsCert(HttpsURLConnection con) { | |
if (con != null) { | |
try { | |
System.out.println("Response Code : " + con.getResponseCode()); | |
System.out.println("Cipher Suite : " + con.getCipherSuite()); | |
System.out.println("\n"); | |
Certificate[] certs = con.getServerCertificates(); | |
for (Certificate cert : certs) { | |
System.out.println("Cert Type : " + cert.getType()); | |
System.out.println("Cert Hash Code : " + cert.hashCode()); | |
System.out.println("Cert Public Key Algorithm : " | |
+ cert.getPublicKey().getAlgorithm()); | |
System.out.println("Cert Public Key Format : " | |
+ cert.getPublicKey().getFormat()); | |
System.out.println("\n"); | |
} | |
} catch (SSLPeerUnverifiedException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment