import java.net.*;  // For Socket stuff
import java.util.*; // For StringTokenizer
import java.io.*;   // For PrintStream

//============================================================================
// Takes a URL as input and retrieves the file. Only handles the http
// protocol. Does a direct connection rather than using the URLConnection
// object in order to illustrate the use of sockets.
//
// 8/96 Marty Hall, hall@apl.jhu.edu
//      http://www.apl.jhu.edu/~hall/java/
//============================================================================

public class GetURL {

  public static int port = 80;
  
  public static void main(String[] args) {
    StringTokenizer tok = new StringTokenizer(args[0]);
    String protocol = tok.nextToken(":");
    String host = tok.nextToken(":/");
    String uri;
    if (tok.hasMoreTokens())
      uri = "/" + tok.nextToken(" ") + "/";
    else
      uri = "/";
    System.out.println("Protocol=" + protocol +  ", host=" + host +
		       ", uri=" + uri);
    try {
      Socket clientSocket = new Socket(host, port);
      PrintStream outStream = new PrintStream(clientSocket.getOutputStream());
      DataInputStream inStream =
	new DataInputStream(clientSocket.getInputStream());
      outStream.println("GET " + uri + " HTTP/1.0\n");
      String line;
      while ((line = inStream.readLine()) != null)
	System.out.println("> " + line);
    } catch(IOException ioe) {
      System.out.println("IOException:" + ioe);
    } catch(Exception e) {
      System.out.println(e.fillInStackTrace());
      System.out.println(e.getMessage());
      e.printStackTrace();
      System.out.println("Error! " + e);
    }
  }
}

//============================================================================

 
