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) 2011, 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:

C:\herong>\local\jdk\bin\java -version

java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)

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

C:\herong>\local\jdk\bin\javac HttpsUrlReader.java

C:\herong>\local\jdk\bin\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.

C:\herong>\local\jdk\bin\javac HttpsUrlReader.java

C:\herong>\local\jdk\bin\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.

Last update: 2018.

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 IE (Internet Explorer)

 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

 Windows Certificate Stores and Console

 .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

 Outdated Tutorials

 References

 Full Version in PDF/EPUB