PRB: Allowing the URL class to Access HTTPS in Applications

ID: Q191120

The information in this article applies to:

SYMPTOMS

Attempting to open a HTTPS: URL in an application with the default URLStreamHandler will result in the following error message being generated:

   java.net.MalformedURLException: unknown protocol: https
           at java/net/URL.<init> (URL.java)

CAUSE

The default URLStreamHandler for applications does not have the ability to access a URL via the HTTPS: protocol.

RESOLUTION

The com.ms.net.wininet.WininetStreamHandlerFactory class can be used to allow applications to access an HTTPS: URL. In an application, you can set the URLStreamHandlerFactory using the following command:

   java.net.URL.setURLStreamHandlerFactory(
      new com.ms.net.wininet.WininetStreamHandlerFactory());

STATUS

This behavior is by design.

MORE INFORMATION

The following Sample code demonstrates how to use the WininetStreamHandlerFactory in a Java application. You should change the line 'String page="..."' to point to a valid HTTPS Web server.

Sample Code

   import java.net.*;
   import java.io.*;

   public class HTTPS
   {

     public static void main(String args[]) {
   /*
     Install a URLStreamHandler that can handle the HTTPS: protocol.
   */ 
       try {
         if (Class.forName(
               "com.ms.net.wininet.WininetStreamHandlerFactory" )!=null)
           URL.setURLStreamHandlerFactory(new
               com.ms.net.wininet.WininetStreamHandlerFactory());
       } catch (ClassNotFoundException ex) {
         System.err.println(
           "Warning: WininetStreamHandlerFactory class not found.");
       }

       try {
   /*
     The following line assumes that you have a HTTPS: server on your
     local machine, with a Login.ASP page in the root directory. You
     can replace the following line with any valid HTTPS: URL.
   */ 
         String page="https://localhost/Login.asp";
         URL url=new URL(page);
         URLConnection connection=url.openConnection();
         connection.setAllowUserInteraction(true);
         DataInputStream dis=new DataInputStream(
                                   connection.getInputStream());
         String input;
         do {
           input=dis.readLine();
           if (input!=null)
           System.out.println(input);
         } while (input!=null);
         dis.close();
       } catch (IOException ex) {
         System.err.println("IOException, make sure the URL is valid.");
         ex.printStackTrace();
       }
     }
   }

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Derek Jamison, Microsoft Corporation Additional query words:
Keywords          : kbnetwork kbJava 
Issue type        : kbprb

Last Reviewed: August 13, 1998