Listing 23.19 is basically the example from Listing 23.15, which just prints out the page found at a given URL. The difference in this example is that it is reading the page from a site that is using SSL. This example does the handshaking with the server and negotiates an encryption algorithm and then gets the text. Note You will need to have all three JSSE jar files included in your system CLASSPATH for this example to work correctly. Listing 23.19 Source Code for PrintHTTPSUrlPage Applicationimport javax.net.ssl.*; import java.io.*; import java.net.*; import java.security.*; public class PrintHTTPSUrlPage { public static void main(String args[]) throws Exception { System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol"); Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider()); // Notice the https protocol here URL url = new URL("https://www.verisign.com" ); URLConnection conn = url.openConnection(); conn.connect(); BufferedReader reader = new BufferedReader( new InputStreamReader( conn.getInputStream() ) ); String data = null; // Keep reading lines until there are no more to read while( (data = reader.readLine()) != null ) { // Just write out the text to the console System.out.println( data ); } } } Troubleshooting Tip If your HTTPS connection takes a long time to connect, see "Using JSSE Takes Forever" in the following "Troubleshooting" section. |