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

This section provides a tutorial example on how to use the openConnection() 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.

If you read the Java documentation on the openStream() method in the java.net.URL class, you will see that openStream() is actually a shorthand for openConnection().getInputStream(). In this tutorial, I want to write another test program to try openConnection() method following these steps:

Here is the actual test program using the javax.net.ssl.HttpsURLConnection class to perform a GET command on an HTTPS server:

/* HttpsUrlConnection.java
 - Copyright (c) 2011, HerongYang.com, All Rights Reserved.
 */
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import javax.net.ssl.HttpsURLConnection;
public class HttpsUrlConnection {
   public static void main(String[] args) {
      String sURL = args[0];
      try {
         URL oURL = new URL(sURL);
         URLConnection con = oURL.openConnection();

// Testing connection type
         if (con instanceof URLConnection) 
            System.out.println("This is a URLConnection.");
         if (con instanceof HttpURLConnection)
            System.out.println("This is a HttpURLConnection.");
         if (con instanceof HttpsURLConnection)
            System.out.println("This is a HttpsURLConnection.");
         System.out.println("Connection object: "+con.toString());

// Reading data
         BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
         String line;
         while ((line = in.readLine()) != null) 
            System.out.println(line);
         in.close();

      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Test 1: Run this test program to connect to https://login.yahoo.com:

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

C:\herong>\local\jdk\bin\java HttpsUrlConnection
   https://login.yahoo.com

This is a URLConnection.
This is a HttpURLConnection.
This is a HttpsURLConnection.
Connection object: 
   sun.net.www.protocol.https.DelegateHttpsURLConnection:
   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">
...

The test program is working! It confirms that https://login.yahoo.com is an HttpsURLConnection.

Test 2: Run this test program to connect to http://www.yahoo.com:

C:\herong>\local\jdk\bin\java HttpsUrlConnection http://www.yahoo.com

This is a URLConnection.
This is a HttpURLConnection.
Connection object: 
   sun.net.www.protocol.http.HttpURLConnection: 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">
  <meta name="description" content="Welcome to Yahoo!, the world's ...

The test program still works! It confirms that http://www.yahoo.com is not an HttpURLConnection.

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