Skip to content

Instantly share code, notes, and snippets.

@cornet
Created September 15, 2011 15:42
Show Gist options
  • Save cornet/1219603 to your computer and use it in GitHub Desktop.
Save cornet/1219603 to your computer and use it in GitHub Desktop.
Java SSL Test
/*
* Makes simple connection to SSL enabled site.
* Useful for debugging SSL Exceptions :)
*
* To Run:
* $ javac SSLTest.java
* $ java -Djavax.net.debug=all SSLTest
*
*/
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
public class SSLTest {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException,
UnknownHostException, IOException {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
Socket s = SSLSocketFactory.getDefault()
.createSocket("github.com", 443);
OutputStream out = s.getOutputStream();
out.write("Say hello".getBytes("UTF8"));
out.close();
s.close();
}
}
@amezick
Copy link

amezick commented May 18, 2018

Thank you very much for this! I was able to quickly debug an issue I was having.
Very much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment