UCDemo.java
Java documentation URLConnection class
// Demonstrate URLConnection. import java.net.*; import java.io.*; import java.util.*; class UCDemo { public static void open(String str) { InputStream in = null; URLConnection connection = null; try { URL url = new URL(str); connection = url.openConnection(); // get date long d = connection.getDate(); if(d==0) System.out.println("No date information."); else System.out.println("Date: " + new Date(d)); // get content type System.out.println("Content-Type: " + connection.getContentType()); // get expiration date d = connection.getExpiration(); if(d==0) System.out.println("No expiration information."); else System.out.println("Expires: " + new Date(d)); // get last-modified date d = connection.getLastModified(); if(d==0) System.out.println("No last-modified information."); else System.out.println("Last-Modified: " + new Date(d)); // get content length int len = connection.getContentLength(); if(len == -1) System.out.println("Content length unavailable."); else System.out.println("Content-Length: " + len); if(len != 0) { System.out.println("=== Content ==="); in = connection.getInputStream(); int ch; while (((ch = in.read()) != -1)) { System.out.print((char) ch); } } else { System.out.println("No content available."); } } catch(IOException exc) { System.out.println("Connection Error: " + exc); } finally { try { if(in != null) in.close(); } catch(IOException exc) { System.out.println("Error closing connection: " + exc); } } } public static void main(String[] args) { open("https://johnloomis.org/ece538"); } }
Date: Wed Nov 14 14:36:27 EST 2012 Content-Type: text/html No expiration information. Last-Modified: Thu Oct 11 15:57:51 EDT 2012 Content-Length: 1290 === Content === Content found here ...
Maintained by John Loomis, updated Wed Nov 14 14:36:42 2012