Skip to content

Instantly share code, notes, and snippets.

@antoniomaria
Last active January 14, 2022 10:37
Show Gist options
  • Save antoniomaria/b50f4e1a6ddbdd171580bc61bca9eb90 to your computer and use it in GitHub Desktop.
Save antoniomaria/b50f4e1a6ddbdd171580bc61bca9eb90 to your computer and use it in GitHub Desktop.
SSLPoke
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Establish a SSL connection to a host and port, writes a byte and
* prints the response. See
* @seen in http://confluence.atlassian.com/display/JIRA/Connecting+to+SSL+services
*/
public class SSLPoke {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: " + SSLPoke.class.getName() + " <host> <port>");
System.exit(1);
}
try {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket(args[0], Integer.parseInt(args[1]));
InputStream in = sslsocket.getInputStream();
OutputStream out = sslsocket.getOutputStream();
// Write a test byte to get a reaction :)
out.write(1);
while (in.available() > 0) {
System.out.print(in.read());
}
System.out.println("Successfully connected");
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
@antoniomaria
Copy link
Author

If connection fails, include server certificate to cacerts

https://github.com/escline/InstallCert/blob/master/InstallCert.java

@antoniomaria
Copy link
Author

Use in case of:

`javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target```

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