Check https connection in Java

Check the https connection from Java, not from the browser. The environment uses Java 8. No other library is used.

Source

package ssltest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.cert.Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;

public class Connect {

	public static void main(String[] args) {
		try {
			URL url = new URL("https://google.co.jp/");
			HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
			conn.connect();
			dispEnv();
			dispContents(url);
			dispCerts(conn);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void dispEnv() {
		try {
			System.out.print("Operating system name(os.name):");
			System.out.println(System.getProperty("os.name"));
			System.out.print("Java version(java.version):");
			System.out.println(System.getProperty("java.version"));

			InetAddress ia = InetAddress.getLocalHost();
			String ip = ia.getHostAddress();
			String hostname = ia.getHostName();
			System.out.println("IP address(getLocalHost):" + ip);
			System.out.println("hostname(hostName):" + hostname);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	public static void dispCerts(HttpsURLConnection conn) {
		System.out.println("----View server certificate chain----");
		try {
			Certificate[] certs = conn.getServerCertificates();
			for (Certificate cert : certs) {
				System.out.println(cert);
			}
		} catch (SSLPeerUnverifiedException e) {
			e.printStackTrace();
		}
	}

	public static void dispContents(URL url) {
		System.out.println("----View content----");
		try {
			InputStream strm = url.openStream();
			InputStreamReader in = new InputStreamReader(strm);
			BufferedReader inb = new BufferedReader(in);
			String line;
			while ((line = inb.readLine()) != null) {
				System.out.println(line);
			}
			inb.close();
			in.close();
			strm.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Execution result

Some of the content is hidden and omitted.

Operating system name(os.name):Windows 10
Java version(java.version):1.8.0_74
IP address(getLocalHost):192.168.XXX.XXX
hostname(hostName):DESKTOP-XXXXX
----View content----
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ja"><head>
・ ・ ・
</body></html>
----View server certificate chain----
[
[
  Version: V3
  Subject: CN=*.google.co.jp, O=Google LLC, L=Mountain View, ST=California, C=US
  Signature Algorithm: SHA256withRSA, OID = 1.2.840.113549.1.1.11
・ ・ ・

In case of error

An error has occurred on the site that I referred to when writing the article.

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
	at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
	at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
	at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
	at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
	at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
	at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
	at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
	at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
	at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
	at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
	at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
	at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
	at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
	at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
	at ssltest.Connect.main(Connect.java:22)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
	at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
	at sun.security.validator.Validator.validate(Validator.java:260)
	at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
	at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
	at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
	at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)
	... 11 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
	at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
	at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
	at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
	... 17 more

Difference from normal

Apparently, the basic limit is marked with △. I will add it if I find out. image.png

Execution jar file

We have prepared a jar file so that you can easily execute it. Please download the following jar file, place it on the server you want to run, and then specify the URL in the argument while passing the java path and use it. https://github.com/koni1212/httpsConnect/raw/master/jarfile/httpsConnect.jar

image.png

Recommended Posts

Check https connection in Java
HTTPS connection using tls1.2 in Java 6
Check Java parameters in Kubernetes pods
Partization in Java
Java version check
Changes in Java 11
Rock-paper-scissors in Java
Java check process
Pi in Java
FizzBuzz in Java
Check static and public behavior in Java methods
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
JAVA DB connection method
Combine arrays in Java
"Hello World" in Java
Check Java toString () implementation
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
HTTPS connection with Java to the self-signed certificate server
Check coverage with Codecov in Java + Gradle + Wercker configuration
Try using RocksDB in Java
Read binary files in Java 1
[Template] MySQL connection with Java
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel
[Neta] Sleep Sort in Java
Edit ini in Java: ini4j
Try DB connection with Java
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
Try developing Spresense in Java (1)