import java.io.*; import java.net.*; import java.util.*; class ImgReader{ public final 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) { System.out.println(" usage: host [dir]"); System.exit(1); } Socket t = new Socket(args[0], PORT); // create a socket 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"; } cmd += "Host: " + args[0].trim() + "\n"; cmd += "Connection: Keep-Alive\n"; // cmd += "User-Agent: self-module\n"; cmd += "UserAgent: self-module\n"; cmd += "Accept: image/gif, image/x-xbitmap, image/jpeg, text/html\n"; cmd += "\r\n\r\n"; 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()))); DataInputStream di = new DataInputStream(t.getInputStream()); PrintStream out = new PrintStream(t.getOutputStream()); out.print(cmd); out.flush(); int count = 0; String inBuffer = new String(); boolean more = true; 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) { int dataLength = Integer.parseInt(length.trim()); byte[] data = new byte[dataLength]; System.out.println("content length : " + dataLength); // contentStream.read(data, 0, 30); di.read(data, 0, dataLength); content = new String(data); File outFile = new File ("out.gif"); FileOutputStream fileWriter = new FileOutputStream(outFile); DataOutputStream myWriter = new DataOutputStream(fileWriter); myWriter.write((byte[])data); System.out.println("past output"); } else { String str; while (more) { str = is.readLine(); if (str == null) more = false; else { content += str; } } // while } /** System.out.println("=== page content begin === "); System.out.print(content); System.out.println("=== page content end === "); **/ /** d = new Date(); // get ready to measure the time start = d.getTime(); while (more) { str = is.readLine(); if (str == null) more = false; else { System.out.println(str); count += str.length(); if (count >= size) more = false; } } // while d = new Date(); end = d.getTime(); System.out.println("total size received : "+ count + " bytes"); System.out.println("transfer rate (Kbytes/seconds) : "+ (double)(count)/(end-start)); System.out.println(" time : " + (end-start) + " ms "); **/ } // 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) {}; // System.out.println("\n end of readAll -> " + inBuffer + "<-"); return inBuffer; } }