/* GetURLInfo.java * * This program uses the URLConnection class to display * information about a URL to the console. * * Derived from "Java Examples in a Nutshell", O'Reilly, 1997. * * Bruce M. Bolden * April 22, 1998 * http://www.cs.uidaho.edu/~bruceb/ */ import java.io.*; import java.net.*; import java.util.Date; public class GetURLInfo { /** Create a URL object. Call printInfo() to display * information about it. */ public static void main( String[] args ) { try { printInfo( new URL( args[0] ) ); } catch ( Exception e ) { System.err.println( e ); System.err.println( "Usage: java GetURLInfo " ); } } /** Use the URLConnection class to get info about the URL */ public static void printInfo( URL url ) throws IOException { // Get URLConnection from the URL URLConnection c = url.openConnection(); // Open a connection to the URL c.connect(); // Display information about the URL contents //System.out.println(" Content Type: " + c.getContentType() ); info(" Content Type: " + c.getContentType() ); info(" Content Encoding: " + c.getContentEncoding() ); info(" Content Length: " + c.getContentLength() ); info(" Date: " + new Date( c.getDate() ) ); info(" Last Modified: " + new Date( c.getLastModified() ) ); info(" Expiration: " + new Date( c.getExpiration() ) ); // Show additional information for an HTTP connection if( c instanceof HttpURLConnection ) { HttpURLConnection h = (HttpURLConnection)c; info(" Request Method: " + h.getRequestMethod() ); info(" Response Message: " + h.getResponseMessage() ); info(" Response Code: " + h.getResponseCode() ); } } static void info( String s ) { System.out.println( s ); } }