Using openStream() Method in java.net.URL Class

This section provides a tutorial example on how to use the openStream() method in the java.net.URL class to perform a GET method on an HTTPS server. Java Secure Socket Extension (JSSE) works quietly behind the java.net.URL class to provide HTTPS support.

According to the JDK documentation, Java Secure Socket Extension (JSSE) works quietly behind other networking API to allow to you communicate with HTTPS servers in the same way as HTTP servers.

One fast way to perform an HTTP GET command is the use the openStream() method in the java.net.URL class as shown in this test program, HttpsUrlReader.java:

/* HttpsUrlReader.java
 * Copyright (c) 2010-2018 HerongYang.com. All Rights Reserved.
 */
import java.io.*;
import java.net.URL;
public class HttpsUrlReader {
   public static void main(String[] args) {
      String sURL = args[0];
      try {
         URL oURL = new URL(sURL);
         BufferedReader in = new BufferedReader(
            new InputStreamReader(oURL.openStream()));
         String line;
         while ((line = in.readLine()) != null)
            System.out.println(line);
         in.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Before doing tests, I want to make sure that I have JDK installed properly:

herong> java -version

java version "15" 2020-09-15
Java(TM) SE Runtime Environment (build 15+36-1562)
Java HotSpot(TM) 64-Bit Server VM (build 15+36-1562, mixed mode, sharing)

For the first test, I want to run this Java program to connect a normal HTTP server, http://www.yahoo.com:

herong> javac HttpsUrlReader.java

herong> java HttpsUrlReader http://www.yahoo.com

<!DOCTYPE html>
<html lang="en-US" class="y-fp-bg y-fp-pg-grad  bkt701">
<!-- m2 template 0 -->
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <title>Yahoo!</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
...

The output confirms that my test Java program, HttpsUrlReader.java, works fine on HTTP servers.

Now let's run this Java program to connect an HTTPS server, https://login.yahoo.com, which we have used in previous tutorials with Internet Explorer and Firefox Web browsers.

herong> java HttpsUrlReader https://login.yahoo.com

<!DOCTYPE html>
<html class="no-js">
<head>
    <title>Yahoo login</title>
    <meta http-equav="Content-Type" content="text/html;charset=utf-8">
    <meta name="referrer" content="origin">
...

Cool. The output confirms that the java.net.URL class automatically uses Java Secure Socket Extension (JSSE) doing extra security tasks required by the HTTPS communication.

Table of Contents

 About This Book

 Introduction of PKI (Public Key Infrastructure)

 Introduction of HTTPS (Hypertext Transfer Protocol Secure)

 Using HTTPS with Google Chrome

 Using HTTPS with Mozilla Firefox

 HTTPS with Microsoft Edge

 Using HTTPS with Apple Safari

 HTTPS with IE (Internet Explorer)

 Android and Server Certificate

 iPhone and Server Certificate

 Windows Certificate Stores and Console

 RDP (Remote Desktop Protocol) and Server Certificate

 macOS Certificate Stores and Keychain Access

 Perl Scripts Communicating with HTTPS Servers

 PHP Scripts Communicating with HTTPS Servers

Java Programs Communicating with HTTPS Servers

 Java Secure Socket Extension (JSSE)

Using openStream() Method in java.net.URL Class

 javax.net.ssl.trustStore System Property

 Default Trusted KeyStore File - cacerts

 PKIX Path Building Failed - No CA Certificate

 Using openConnection() Method in java.net.URL Class

 .NET Programs Communicating with HTTPS Servers

 CAcert.org - Root CA Offering Free Certificates

 PKI CA Administration - Issuing Certificates

 Comodo Free Personal Certificate

 Digital Signature - Microsoft Word

 Digital Signature - OpenOffice.org 3

 S/MIME and Email Security

 PKI (Public Key Infrastructure) Terminology

 Archived Tutorials

 References

 Full Version in PDF/EPUB