UrlRetriever.java Accepts an URL from the command line, parses the host, port, and URI components from the URL and then retrieves the document. Requires the following classes: import java.util.*; /** This parses the input to get a host, port, and file, then * passes these three values to the UriRetriever class to * grab the URL from the Web. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class UrlRetriever { public static void main(String[] args) { checkUsage(args); StringTokenizer tok = new StringTokenizer(args[0]); String protocol = tok.nextToken(":"); checkProtocol(protocol); String host = tok.nextToken(":/"); String uri; int port = 80; try { uri = tok.nextToken(""); if (uri.charAt(0) == ':') { tok = new StringTokenizer(uri); port = Integer.parseInt(tok.nextToken(":/")); uri = tok.nextToken(""); } } catch(NoSuchElementException nsee) { uri = "/"; } UriRetriever uriClient = new UriRetriever(host, port, uri); uriClient.connect(); } /** Warn user if the URL was forgotten. */ private static void checkUsage(String[] args) { if (args.length != 1) { System.out.println("Usage: UrlRetriever "); System.exit(-1); } } /** Tell user that this can only handle HTTP. */ private static void checkProtocol(String protocol) { if (!protocol.equals("http")) { System.out.println("Don't understand protocol " + protocol); System.exit(-1); } } } UriRetriever.java Given a host, port, and URI, retrieves the document from the HTTP server. import java.net.*; import java.io.*; /** Retrieve a URL given the host, port, and file as three * separate command-line arguments. A later class * (UrlRetriever) supports a single URL instead. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class UriRetriever extends NetworkClient { private String uri; public static void main(String[] args) { UriRetriever uriClient = new UriRetriever(args[0], Integer.parseInt(args[1]), args[2]); uriClient.connect(); } public UriRetriever(String host, int port, String uri) { super(host, port); this.uri = uri; } /** Send one GET line, then read the results one line at a * time, printing each to standard output. */ // It is safe to use blocking IO (readLine), since // HTTP servers close connection when done, resulting // in a null value for readLine. protected void handleConnection(Socket uriSocket) throws IOException { PrintWriter out = SocketUtil.getWriter(uriSocket); BufferedReader in = SocketUtil.getReader(uriSocket); out.println("GET " + uri + " HTTP/1.0\r\n"); String line; while ((line = in.readLine()) != null) { System.out.println("> " + line); } } } NetworkClient.java import java.net.*; import java.io.*; /** A starting point for network clients. You'll need to * override handleConnection, but in many cases connect can * remain unchanged. It uses SocketUtil to simplify the * creation of the PrintWriter and BufferedReader. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class NetworkClient { protected String host; protected int port; /** Register host and port. The connection won't * actually be established until you call * connect. */ public NetworkClient(String host, int port) { this.host = host; this.port = port; } /** Establishes the connection, then passes the socket * to handleConnection. */ public void connect() { try { Socket client = new Socket(host, port); handleConnection(client); } catch(UnknownHostException uhe) { System.out.println("Unknown host: " + host); uhe.printStackTrace(); } catch(IOException ioe) { System.out.println("IOException: " + ioe); ioe.printStackTrace(); } } /** This is the method you will override when * making a network client for your task. * The default version sends a single line * ("Generic Network Client") to the server, * reads one line of response, prints it, then exits. */ protected void handleConnection(Socket client) throws IOException { PrintWriter out = SocketUtil.getWriter(client); BufferedReader in = SocketUtil.getReader(client); out.println("Generic Network Client"); System.out.println ("Generic Network Client:\n" + "Made connection to " + host + " and got '" + in.readLine() + "' in response"); client.close(); } /** The hostname of the server we're contacting. */ public String getHost() { return(host); } /** The port connection will be made on. */ public int getPort() { return(port); } } SocketUtil.java Provides utilities for wrapping a BufferedReader and PrintWriter around the Socket's input and output streams, respectively. import java.net.*; import java.io.*; /** A shorthand way to create BufferedReaders and * PrintWriters associated with a Socket. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class SocketUtil { /** Make a BufferedReader to get incoming data. */ public static BufferedReader getReader(Socket s) throws IOException { return(new BufferedReader( new InputStreamReader(s.getInputStream()))); } /** Make a PrintWriter to send outgoing data. * This PrintWriter will automatically flush stream * when println is called. */ public static PrintWriter getWriter(Socket s) throws IOException { // Second argument of true means autoflush. return(new PrintWriter(s.getOutputStream(), true)); } }
Aug 24