/* This program connects to a web server and gets infor from it. * It retrives the root page "/" if no other specifc page is given, * or it retrives the specified 'dir' * page. * Apr-1998 X. Meng * sep-03-2002 X. Meng replace the DataInputStream with BufferedReader * nov-12-2002 X. Meng consider the cases where the content-length is not * returned by the server. */ import java.io.*; import java.net.*; import java.util.*; class TcpHttpClient { public final static int PORT = 80; // default web port public final static String CMD = "HEAD"; public static void main(String[] args) throws IOException { Date d; long start, end; if (args.length != 1) { System.out.println(" usage: java TcpHttpClient host"); System.exit(1); } Socket t = new Socket(args[0].trim(), PORT); // create a socket String cmd = new String(CMD + " /"); cmd += " HTTP/1.1\n"; cmd += "Host: " + args[0].trim() + "\n"; cmd += "\r\n\r\n"; System.out.println("sending command :"); System.out.print(cmd); // connecting to socket input and output BufferedReader is = new BufferedReader (new InputStreamReader (new DataInputStream(t.getInputStream()))); PrintStream out = new PrintStream(t.getOutputStream()); out.print(cmd); out.flush(); String inBuffer = new String(); inBuffer = readHttpHeader(is); System.out.println("header : " + inBuffer); } // main public static String readHttpHeader(BufferedReader fileIn) { String inBuffer = ""; try { String line; do { line = fileIn.readLine(); inBuffer += line + "\r\n"; // System.out.print("in readAll -> " + line + "<-"); if (line.length() == 2 && line.compareTo("\n\n") == 0) { System.out.println("we hit the break in readAll"); break; } } while (line.length() > 0); } catch (IOException ignored) {}; return inBuffer; } }