JDK Tutorials - Herong's Tutorial Examples - Version 5.11, by Dr. Herong Yang

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) 2005 by Dr. Herong Yang
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: "
      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());

Of course, we have to run HttpsClient.java with the server's certificate in a trusted key store file:

>\jdk\bin\java -cp . "-Djavax.net.ssl.trustStore=public.jks" 

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/
   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.

Last update: 2006.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Downloading and Installing JDK 1.5.0 on Windows

 Downloading and Installing JDK 1.4.1 on Windows

 Downloading and Installing JDK 1.3.1 on Windows

 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

 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 - Secret Key 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

 Connecting to HttpsHello.java with IE

 HttpsEchoer.java - A Better HTTPS Server


 PDF Printing Version

HttpsClient.java - HTTPS Client Test Program - Updated in 2014, by Dr. Herong Yang