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. * @see SocketUtil * @see UrlRetriever */ 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.getPrintWriter(uriSocket); BufferedReader in = SocketUtil.getBufferedReader(uriSocket); out.println("GET " + uri + " HTTP/1.0\n"); String line; while ((line = in.readLine()) != null) System.out.println("> " + line); } }