- HTTPS Client Test Program

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

To test presented in the previous section, I wrote a simple HTTPS client program,

 * Copyright (c) All Rights Reserved.
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: "
      try {
      // Getting the default SSL socket factory
         SSLSocket c =
           (SSLSocket) f.createSocket("localhost", 8888);
         BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
         BufferedReader r = new BufferedReader(new InputStreamReader(
         w.write("GET / HTTP/1.0");
         w.newLine(); // end of HTTP request
         String m = null;
         while ((m=r.readLine())!= null) {
      } catch (IOException e) {
   private static void printSocketInfo(SSLSocket s) {
      System.out.println("Socket class: "+s.getClass());
      System.out.println("   Remote address = "
      System.out.println("   Remote port = "+s.getPort());
      System.out.println("   Local socket address = "
      System.out.println("   Local address = "
      System.out.println("   Local port = "+s.getLocalPort());
      System.out.println("   Need client authentication = "
      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 with the server's certificate in a trusted key store file.

Here is how the program works with JDK 1.7:

herong> java

The default SSL socket factory class:
Socket class: class
   Remote address = localhost/
   Remote port = 8888
   Local socket address = /
   Local address = /
   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? - HTTPS Server Test Program - HTTPS Client Test Program Failed with JDK 1.8

 Using SO_LINGER Socket Option

 HTTPS Server with Expired Certificate

 Connecting to with IE - A Better HTTPS Server

 Outdated Tutorials


 Full Version in PDF/EPUB