/* * Copyright (c) 2001 Matthew Feldt. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided the copyright notice above is * retained. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND WITHOUT ANY EXPRESSED OR * IMPLIED WARRANTIES. */ /** * URLModDate.java * * Java Examples In A Nutshell Copyright (c) 2000 David Flanagan * Exercise 5-1: * Using the URLConnection techniques demonstrated in Example 5-2, write a * program that prints the modification date of a specified URL. * * @author Matthew Feldt * @version 1.0, 03/02/2001 17:05 */ package com.feldt.examples.net; import java.net.*; import java.io.*; import java.util.*; public class URLModDate { public static void printModifiedDate(URL url) throws IOException { URLConnection c = url.openConnection(); // get URLConnection from url c.connect(); // open a connection to url // if its an HTTP connection display header info if (c instanceof HttpURLConnection) { HttpURLConnection h = (HttpURLConnection)c; System.out.println(" Request Method: " + h.getRequestMethod()); System.out.println(" Response Message: " + h.getResponseMessage()); System.out.println(" Response Code: " + h.getResponseCode()); } // display the last modified date System.out.print(" Last Modified: "); if (c.getLastModified() == 0) System.out.println("Not known."); else System.out.println(new Date(c.getLastModified())); } public static void main(String[] args) { final String usage = "Usage: java URLModDate "; if (args.length != 1) { System.err.println(usage); System.exit(-1); } try { printModifiedDate(new URL(args[0])); } catch(IOException e) { System.err.println(e.getMessage()); System.err.println(usage); System.exit(-1); } } }