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 "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
Java HotSpot(TM) Client VM (build 17.0-b17, mixed mode, sharing)

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 PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3....
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sign in to Yahoo!</title>

<meta http-equiv="Pragma" content="no-cache">
...

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: 2011.

Table of Contents

 About This Book

 Introduction of PKI (Public Key Infrastructure)

 Introduction of HTTPS (Hypertext Transfer Protocol Secure)

 Using HTTPS with IE (Internet Explorer) 10

 Using HTTPS with Chrome 40

 Using HTTPS with Firefox 35

 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

 Certificate Stores and Certificate Console

 .NET Programs Communicating with HTTPS Servers

 CAcert.org - Root CA Offering Free Certificates

 PKI CA Administration - Issuing Certificates

 Digital Signature - Microsoft Word 2007

 Digital Signature - OpenOffice.org 3

 S/MIME and Email Security

 PKI (Public Key Infrastructure) Terminology

 Outdated Tutorials

 References

 PDF Printing Version