URLReader2.java
URLConnection
01: import java.net.*; 02: import java.io.*; 03: 04: public class URLReader2 { 05: public static void main(String[] args) { 06: URL url; 07: URLConnection con; 08: int n, nlines; 09: String name; 10: if (args.length>0) name = args[0]; 11: else name = "https://johnloomis.org"; 12: if (args.length>1) nlines = Integer.parseInt(args[1]); 13: else nlines = 5; 14: System.out.println("Opening: " + name); 15: try { 16: url = new URL(name); 17: con = url.openConnection(); 18: // check header fields 19: for (n=0; n<100; n++) { 20: String key = con.getHeaderFieldKey(n); 21: if (key!=null) System.out.println(key + ": " + con.getHeaderField(n)); 22: else if (n==0) System.out.println("Status: " + con.getHeaderField(0)); 23: else break; 24: } 25: BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); 26: String inp; 27: System.out.println("\nShowing first few lines of content:\n"); 28: for (n=0; n<nlines; n++) { 29: inp = in.readLine(); 30: if (inp==null) break; 31: System.out.println(inp); 32: } 33: in.close(); 34: } 35: catch (MalformedURLException e) { 36: System.out.println(e); 37: } 38: catch (IOException e) { 39: System.out.println(e); 40: e.printStackTrace(); 41: } 42: } 43: }
An URLConnection
allows us to examine
header information as well as the content.
C:\prog\java_web>java URLReader2 Opening: https://johnloomis.org Status: HTTP/1.1 200 OK Date: Wed, 20 Feb 2008 20:59:23 GMT Server: Apache Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Transfer-Encoding: chunked Content-Type: text/html Showing first few lines of content: <html> <head><title>Dr. John S. Loomis</title> </head> <body> <P>
Maintained by John Loomis, updated Wed Feb 20 15:59:40 2008