Core Java

Java HTTPS Client

Hello. In this tutorial, we will make an https client call from java and log the output on the console.

1. Introduction

Java8 programming language supports for TLS 1.2 version where TLS defines the transport-level security and is a successor to the SSL (Secure sockets layer) protocol. It is a cryptographic protocol designed to provide secure communication over the computer network. This protocol is actively used in applications such as email, voip, and instant messages.

1.1 Server certificates

Server certificates are used to identify the server. When installed on the server the ssl certificate helps to secure the website from http to https.

1.2 Client certificates

Client certificates are digital certificates used to authenticate the requestor’s identity. This certificate ensures the server is communicating with a legit user.

2. Practice

Let us dive into some practice stuff from here and I am assuming that you already have the Java 1.8 or greater installed on your local machine. I am using JetBrains IntelliJ IDEA as my preferred IDE. You’re free to choose the IDE of your choice. The below pictures shows the project structure.

Fig. 1: Project structure

2.1 Setting up the pom

Add the following code to the pom file. Since this is a simple java application so we will not use any fancy dependencies and will simply set the java version property.

pom.xml

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://maven.apache.org/POM/4.0.0"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <artifactId>javahttpsclient</artifactId>
  <groupId>com.learning</groupId>
  <modelVersion>4.0.0</modelVersion>
  <name>java https client</name>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <version>0.0.1-SNAPSHOT</version>
</project>

2.2 Creating the implementation class

Add the following code to the java class created in the com.learning package under the src/main/java folder. The code will connect to google via the secure protocol (https) and print the response on the console.

HttpsClient.java

package com.learning;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class HttpsClient {

  private static void connect() {
    final String endpoint = "https://www.google.com";
    URL url;
    try {
      url = new URL(endpoint);
      HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
      printContent(connection);
    } catch (IOException urlException) {
      urlException.printStackTrace();
    }
  }

  private static void printContent(HttpsURLConnection connection) {
    if (connection != null) {
      try {
        System.out.println("Response code= " + connection.getResponseCode() + "\n");
        System.out.println("Printing url content\n");
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String input;
        while ((input = br.readLine()) != null) {
          System.out.println(input);
        }
        br.close();
      } catch (IOException ioException) {
        ioException.printStackTrace();
      }
    }
  }

  public static void main(String[] args) {
    connect();
  }
}

Run the file as a java file and if everything goes well the following logs will be shown on the IDE console.

Console output

Response code= 200

Printing url content

<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-IN"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script nonce="rEWy779sS-QbpTWn5rky4g">(function(){window.google={kEI:'pER-YvXhLK232roP-5OB0AM',kEXPI:'0,1302530,56878,6060,206,4804,2316,383,246,5,1354,4013,1237,1122516,1197714,680,380096,16115,28684,17572,4859,1361,9291,3023,17585,4020,978,13228,3847,4192,6430,22741,1832,3249,1593,1279,2742,149,1103,840,6297,109,3405,606,2025,1775,520,14670,3227,2845,7,5599,11851,15767,553,1850,6398,9359,3,576,1014,1,5444,149,11323,2652,4,1528,2304,7039,22023,3050,2658,4163,3194,13658,21223,5815,2542,4094,17,4035,3,3541,1,11943,30211,2,14022,1931,92,692,255,11146,9865,1758,5679,1021,2378,2720,11306,6937,2,2,5,7772,4567,6256,2981,3739,16701,1252,5835,5658,2,9307,1555,2778,8,6081,1395,445,2,2,1,17312,7466,1417,1,436,8155,6582,323,476,2,10,2437,511,83,858,10779,7341,3217,186,2,2,5,12972,541,229,265,846,172,1224,2930,415,918,1138,1125,878,1391,4249,346,1325,253,1363,1,351,536,1366,3431,2,208,490,4,998,1914,1724,2039,6247,418,200,876,1169,315,963,146,144,239,1242,418,3,217,147,515,2,95,1236,62,133,3570,479,1,470,17,48,502,2714,1,4,

-- long out snipped for brevity –

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we discussed the practical implementation of the java https client. You can download the source code from the Downloads section.

4. Download the Project

This was a tutorial to implement the https client call in java.

Download
You can download the full source code of this example here: Java HTTPS Client

 

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button