001: // HttpClient.java
002: // $Id: HttpClient.java,v 1.4 2000/08/16 21:38:02 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.mux.tests;
007:
008: import java.io.BufferedOutputStream;
009: import java.io.DataOutputStream;
010: import java.io.IOException;
011: import java.io.InputStream;
012: import java.io.OutputStream;
013: import java.io.PrintStream;
014:
015: import java.net.Socket;
016: import java.net.URL;
017:
018: import java.util.Vector;
019:
020: import org.w3c.www.mux.MuxSession;
021: import org.w3c.www.mux.MuxStream;
022:
023: class MuxFetcher extends Thread {
024: MuxSession ses = null;
025: DataOutputStream out = null;
026: InputStream in = null;
027: URL u = null;
028: OutputStream dst = null;
029:
030: public void request(URL u) throws IOException {
031: out.writeBytes("GET " + u + " HTTP/1.1\r\n");
032: out.writeBytes("Connection: close\r\n\r\n");
033: out.flush();
034: }
035:
036: public void copy(URL u, OutputStream dst) throws IOException {
037: request(u);
038: byte buffer[] = new byte[1024];
039: int got = -1;
040: while ((got = in.read(buffer, 0, buffer.length)) > 0)
041: dst.write(buffer, 0, got);
042: }
043:
044: public void dump(URL u) throws IOException {
045: request(u);
046: byte buffer[] = new byte[1024];
047: int got = -1;
048: while ((got = in.read(buffer, 0, buffer.length)) > 0)
049: System.out.print(new String(buffer, 0, 0, got));
050: System.out.println("Dumped all available data !");
051: }
052:
053: public void run() {
054: try {
055: // Get the streams:
056: out = (new DataOutputStream(new BufferedOutputStream(ses
057: .getOutputStream())));
058: in = ses.getInputStream();
059: // Run the command:
060: if (dst != null)
061: copy(u, dst);
062: else
063: dump(u);
064: ses.shutdown();
065: } catch (Exception ex) {
066: ex.printStackTrace();
067: }
068: }
069:
070: MuxFetcher(MuxStream stream, URL u) throws IOException {
071: this (stream, u, null);
072: }
073:
074: MuxFetcher(MuxStream stream, URL u, OutputStream dst)
075: throws IOException {
076: this .ses = stream.connect(80);
077: this .dst = dst;
078: this .u = u;
079: start();
080: }
081:
082: }
083:
084: public class HttpClient {
085: public static MuxStream stream = null;
086:
087: public static void main(String args[]) {
088: String host = "www43.inria.fr";
089: int port = 8007;
090: Vector urls = new Vector();
091:
092: // Parse command line:
093: for (int i = 0; i < args.length; i++) {
094: if (args[i].equals("-h")) {
095: host = args[++i];
096: } else if (args[i].equals("-p")) {
097: try {
098: port = Integer.parseInt(args[++i]);
099: } catch (Exception ex) {
100: ex.printStackTrace();
101: }
102: } else {
103: urls.addElement(args[i]);
104: }
105: }
106: // Connect to target host:
107: try {
108: Socket socket = new Socket(host, port);
109: MuxStream stream = new MuxStream(false, null, socket);
110: MuxFetcher fetchers[] = new MuxFetcher[urls.size()];
111: // Fork the fetchers:
112: for (int i = 0; i < urls.size(); i++) {
113: URL u = new URL((String) urls.elementAt(i));
114: fetchers[i] = new MuxFetcher(stream, u);
115: }
116: // Wait for completion:
117: for (int i = 0; i < fetchers.length; i++) {
118: fetchers[i].join();
119: }
120: // Close mux stream:
121: stream.shutdown(true);
122: } catch (Exception ex) {
123: ex.printStackTrace();
124: }
125:
126: }
127:
128: }
|