Better Example

import java.net.*;
import java.io.*;

public class BetterCat {

  public static void main(String[] args) {

    for (int i = 0; i < args.length; i++) {  
      int port = 80;
      String file = "/";
      try {
        URL u = new URL(args[i]);
        if (u.getPort() != -1) port = u.getPort();
        if (!(u.getProtocol().equalsIgnoreCase("http"))) {
          System.err.println("I only understand http.");
        }
        if (!(u.getFile().equals(""))) file = u.getFile();
        Socket s = new Socket(u.getHost(), port);
        OutputStream theOutput = s.getOutputStream();
        OutputStreamWriter out = new OutputStreamWriter(theOutput);
        out.write("GET " + file + " HTTP/1.0\r\n");
        out.write("Accept: text/plain, text/html, text/*\r\n");
        out.write("\r\n");
        out.flush();
        theOutput.flush();
        
        InputStream in = s.getInputStream();
        InputStreamReader isr = new InputStreamReader(in);
        BufferedReader br = new BufferedReader(isr);
        int c;
        while ((c = br.read()) != -1) {
          System.out.print((char) c);
        }
      }
      catch (MalformedURLException ex) {
        System.err.println(args[i] + " is not a valid URL");
      }
      catch (IOException ex) {
        System.err.println(ex);
      }
      
    }
  
  }

}

Previous | Next | Top | Cafe con Leche

Copyright 2003 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified February 13, 2003