URLConnectionTest.java

C:\ece538\CoreJava10_vol2\v2ch04\urlConnection>java URLConnectionTest
Keep-Alive: timeout=5, max=100
Accept-Ranges: bytes
null: HTTP/1.1 200 OK
Server: Apache/2.2.24 (Unix) mod_ssl/2.2.24 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 mod_fcgid/2.3.6 Sun-ONE-ASP/4.0.3
ETag: "2590e1c-1c4c-53af29319ee00"
Connection: Keep-Alive
Last-Modified: Fri, 26 Aug 2016 05:13:28 GMT
Content-Length: 7244
Date: Wed, 12 Apr 2017 17:50:28 GMT
Content-Type: text/html
----------
getContentType: text/html
getContentLength: 7244
getContentEncoding: null
getDate: 1492019428000
getExpiration: 0
getLastModifed: 1472188408000
----------



  Cay Horstmann's Home Page
  
  



Welcome to Cay Horstmann’s Home Page!Photo of Cay Horstmann

. . .


URLConnectionTest.java

import java.io.*;
import java.net.*;
import java.nio.charset.*;
import java.util.*;

/**
 * This program connects to an URL and displays the response header data and the first 10 lines of
 * the requested data.
 * 
 * Supply the URL and an optional username and password (for HTTP basic authentication) on the
 * command line.
 * @version 1.11 2007-06-26
 * @author Cay Horstmann
 */
public class URLConnectionTest
{
   public static void main(String[] args)
   {
      try
      {
         String urlName;
         if (args.length > 0) urlName = args[0];
         else urlName = "http://horstmann.com";

         URL url = new URL(urlName);
         URLConnection connection = url.openConnection();

         // set username, password if specified on command line

         if (args.length > 2)
         {
            String username = args[1];
            String password = args[2];
            String input = username + ":" + password;
            Base64.Encoder encoder = Base64.getEncoder();
            String encoding = encoder.encodeToString(input.getBytes(StandardCharsets.UTF_8));
            connection.setRequestProperty("Authorization", "Basic " + encoding);
         }

         connection.connect();

         // print header fields

         Map<String, List<String>> headers = connection.getHeaderFields();
         for (Map.Entry<String, List<String>> entry : headers.entrySet())
         {
            String key = entry.getKey();
            for (String value : entry.getValue())
               System.out.println(key + ": " + value);
         }

         // print convenience functions

         System.out.println("----------");
         System.out.println("getContentType: " + connection.getContentType());
         System.out.println("getContentLength: " + connection.getContentLength());
         System.out.println("getContentEncoding: " + connection.getContentEncoding());
         System.out.println("getDate: " + connection.getDate());
         System.out.println("getExpiration: " + connection.getExpiration());
         System.out.println("getLastModifed: " + connection.getLastModified());
         System.out.println("----------");

         String encoding = connection.getContentEncoding();
         if (encoding == null) encoding = "UTF-8";
         try (Scanner in = new Scanner(connection.getInputStream(), encoding))
         {   
            // print first ten lines of contents
   
            for (int n = 1; in.hasNextLine() && n <= 10; n++)
               System.out.println(in.nextLine());
            if (in.hasNextLine()) System.out.println(". . .");
         }
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
   }   
}


Maintained by John Loomis, updated Wed Apr 12 13:51:38 2017