Skip to content

Instantly share code, notes, and snippets.

@terabyte
Created January 8, 2013 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terabyte/4480197 to your computer and use it in GitHub Desktop.
Save terabyte/4480197 to your computer and use it in GitHub Desktop.
Messing with Java SSL I Created keystore using this command: $ keytool -genkeypair -alias newkey -keystore someKeyStore ==== Keystore looks like this: $ keytool -list -keystore someKeyStore Enter keystore password: Keystore type: JKS Keystore provider: SUN Your keystore contains 1 entry newkey, Jan 7, 2013, PrivateKeyEntry, Certificate fingerpri…
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import org.apache.commons.lang.StringUtils;
public class HTTPSTest {
// stolen from https://secure.marumoto.us/motowiki/tiki-index.php?page=Test+a+Java+SSL+connection
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java HTTPSClient host");
return;
}
int port = 443; // default https port
String host = args[0];
if (host.contains(":")) {
String[] parts = StringUtils.split(host, ":");
host = parts[0];
port = Integer.valueOf(parts[1]);
}
System.out.println("Connecting to host '" + host + "' port " + Integer.toString(port));
try {
// Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
Writer out = new OutputStreamWriter(socket.getOutputStream());
// https requires the full URL in the GET line
out.write("GET / HTTP/1.0\\r\\\n");
out.write("\\r\\n");
out.flush();
// read response
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
int c;
while ((c = in.read()) != -1) {
System.out.write(c);
}
out.close();
in.close();
socket.close();
} catch (IOException e) {
System.err.println(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment