// (C) 2005 Richard Atterer, GPLv2
/*
GET /~richard/ HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050922 Firefox/1.0.7 (Debian package 1.0.7-1)
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*XXX/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
*/

import java.net.*;
import java.io.*;

class HttpClient {
    
    public static void main(String args[])
        throws java.io.IOException {

	String host = "localhost";
	int port = 80;
        String path = "/~richard/";

	System.out.println("Connecting to \"" + host + ":" + port + "\"...");
	Socket sock = new Socket(host, port);
	InputStream isock = sock.getInputStream();
	OutputStream osock = sock.getOutputStream();
	System.out.println("Connected.");

        writeHeader(osock, "GET " + path + " HTTP/1.0");
        writeHeader(osock, "");

        // Show server's reaction
        int servChar = 0;
        do {
            servChar = isock.read();
            if ((servChar >= 0 && servChar < 32 && servChar != 10)
                || (servChar >= 127 && servChar < 160))
                System.out.print("[" + servChar + "]");
            else
                System.out.write(servChar);
        } while (servChar != -1);

        sock.close();
    }

    /** Write s to o as HTTP header, i.e. followed by \r\n */
    private static void writeHeader(OutputStream o, String s)
        throws java.io.IOException {
        o.write(s.getBytes());
        o.write(crlf);
    }
    private static byte[] crlf = { 0x0d, 0x0a };

}
