Skip to content

Instantly share code, notes, and snippets.

@malys
Forked from 4ndrej/SSLPoke.java
Last active April 25, 2019 06:58
Show Gist options
  • Save malys/0c4ce2e096ca1ea43e080637648178b5 to your computer and use it in GitHub Desktop.
Save malys/0c4ce2e096ca1ea43e080637648178b5 to your computer and use it in GitHub Desktop.
Test of java SSL / keystore / cert setup. Check the commet #1 for howto.
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
/** Establish a SSL connection to a host and port, writes a byte and
* prints the response. See
* http://confluence.atlassian.com/display/JIRA/Connecting+to+SSL+services
*/
public class SSLPoke {
public static void main(String[] args) {
String host;
String port;
if (args.length != 2) {
System.out.println("Usage: "+SSLPoke.class.getName()+" <host> <port>");
System.exit(1);
}else{
host=args[0];
port=args[1];
}
try {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket(host, Integer.parseInt(port));
InputStream ina = sslsocket.getInputStream();
OutputStream out = sslsocket.getOutputStream();
// Write a test byte to get a reaction :)
out.write(1);
while (ina.available() > 0) {
System.out.print(ina.read());
}
System.out.println("Successfully connected");
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment