HttpsClient.java - HTTPS Client Test Program

This section provides a tutorial example on how to write a HTTPS client test program, HttpsClient.java. It sends a simple HTTP request page using the HTTPS protocol.

To test HttpsHello.java presented in the previous section, I wrote a simple HTTPS client program, HttpsClient.java:

/* HttpsClient.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
public class HttpsClient {
   public static void main(String[] args) {
      PrintStream out = System.out;

      // Getting the default SSL socket factory
      SSLSocketFactory f =
         (SSLSocketFactory) SSLSocketFactory.getDefault();
      out.println("The default SSL socket factory class: "
         +f.getClass());
      try {
      // Getting the default SSL socket factory
         SSLSocket c =
           (SSLSocket) f.createSocket("localhost", 8888);
         printSocketInfo(c);
         c.startHandshake();
         BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
            c.getOutputStream()));
         BufferedReader r = new BufferedReader(new InputStreamReader(
            c.getInputStream()));
         w.write("GET / HTTP/1.0");
         w.newLine();
         w.newLine(); // end of HTTP request
         w.flush();
         String m = null;
         while ((m=r.readLine())!= null) {
            out.println(m);
         }
         w.close();
         r.close();
         c.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   private static void printSocketInfo(SSLSocket s) {
      System.out.println("Socket class: "+s.getClass());
      System.out.println("   Remote address = "
         +s.getInetAddress().toString());
      System.out.println("   Remote port = "+s.getPort());
      System.out.println("   Local socket address = "
         +s.getLocalSocketAddress().toString());
      System.out.println("   Local address = "
         +s.getLocalAddress().toString());
      System.out.println("   Local port = "+s.getLocalPort());
      System.out.println("   Need client authentication = "
         +s.getNeedClientAuth());
      SSLSession ss = s.getSession();
      System.out.println("   Cipher suite = "+ss.getCipherSuite());
      System.out.println("   Protocol = "+ss.getProtocol());
   }
}

This program works correct with JDK 1.7 and older versions. Of course, we have to run HttpsClient.java with the server's certificate in a trusted key store file.

Here is how the program works with JDK 1.7:

herong> java -Djavax.net.ssl.trustStore=public.jks HttpsClient.java

The default SSL socket factory class:
   class com.sun.net.ssl.internal.ssl.SSLSocketFactoryImpl
Socket class: class com.sun.net.ssl.internal.ssl.SSLSocketImpl
   Remote address = localhost/127.0.0.1
   Remote port = 8888
   Local socket address = /127.0.0.1:2408
   Local address = /127.0.0.1
   Local port = 2408
   Need client authentication = false
   Cipher suite = TLS_DHE_DSS_WITH_AES_128_CBC_SHA
   Protocol = TLSv1
HTTP/1.0 200 OK
Content-Type: text/html

<html><body>Hello world!</body></html>

The result matches my expectation perfectly.

I got the same result with JDK 1.6 and 1.5.

However, this program is not working any more in JDK 1.8 and newer versions. See next section for more details.

Table of Contents

 About This JDK Tutorial Book

 JDK (Java Development Kit)

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Java Logging

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

 SSL Client Authentication

HTTPS (Hypertext Transfer Protocol Secure)

 What Is HTTPS?

 HttpsHello.java - HTTPS Server Test Program

HttpsClient.java - HTTPS Client Test Program

 HttpsClient.java Failed with JDK 1.8

 Using SO_LINGER Socket Option

 HTTPS Server with Expired Certificate

 Connecting to HttpsHello.java with IE

 HttpsEchoer.java - A Better HTTPS Server

 Outdated Tutorials

 References

 Full Version in PDF/EPUB