/* * 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.

* * @author Xiannong Meng * Date: September 7, 2004

*/ import java.io.*; import java.net.*; import java.util.*; class WebClient { public static int PORT = 80; // default web port public static void main(String[] args) throws IOException { Date d; long start, end; if (args.length != 1 && args.length != 2 && args.length != 3) { System.out.println(" usage: host [dir] [port]"); System.exit(1); } String cmd; if (args.length == 1) // get the root page cmd = new String("GET / HTTP/1.0\n"); else { // get a specific page cmd = new String("GET /"); cmd += args[1].trim(); cmd += " HTTP/1.0\n"; } if (args.length == 3) { PORT = Integer.parseInt(args[2].trim()); } Socket t = new Socket(args[0], PORT); // create a socket // construct the rest of the command cmd += "Host: " + args[0].trim() + "\n"; cmd += "Connection: Keep-Alive\n"; // cmd += "User-Agent: self-module\n"; cmd += "UserAgent: self-module\n"; cmd += "Accept: text/html\n"; cmd += "Accept: image/gif\n"; cmd += "Accept: image/x-xbitmap\n"; cmd += "Accept: image/jpeg\n"; cmd += "\r\n\r\n"; // echo to local screen System.out.println("sending command :"); System.out.println(cmd); // connecting to socket input and output BufferedReader is = new BufferedReader (new InputStreamReader (new DataInputStream(t.getInputStream()))); PrintStream out = new PrintStream(t.getOutputStream()); // actually send the command out.print(cmd); out.flush(); int count = 0; String inBuffer = new String(); boolean more = true; // read the header portion of the response from the server inBuffer = readHttpHeader(is); System.out.println("header : " + inBuffer); String content = ""; Hashtable http_data; // HTTP header infromation from client http_data = Http.ParseToken(inBuffer); String length = (String)http_data.get("content-length"); if (length != null) // the server returned the content length { int dataLength = Integer.parseInt(length.trim()); char[] data = new char[dataLength]; System.out.println("content length : " + dataLength); // contentStream.read(data, 0, 30); is.read(data, 0, dataLength); content = new String(data); } else // the server didn't return the content length tag { String str = ""; while (more) { // read until no more str = is.readLine(); if (str == null) more = false; else content += str; } // while } // close the connection is.close(); /** System.out.println("=== page content begin === "); System.out.print(content); System.out.println("=== page content end === "); **/ System.out.println("Content length : " + content.length()); } // main /* * Returns the header portion of the web server response. * The response is ended by two consecutive new line and carriage * return pairs.

* * @param fileIn a pointer to the input buffered reader * @author Xianonng Meng * */ 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) {}; // System.out.println("\n end of readAll -> " + inBuffer + "<-"); return inBuffer; } }