/* Vernetzte Systeme, WS 2001/2002, Übung 5, Aufgabe 21 a * * HTTP/1.0-Protokoll: * HTTP GET-Request mit Hilfe der Socket-Klasse. * * Pointers: * * - Mehr über Lesen und Schreiben von/auf Datenströmen: * http://java.sun.com/docs/books/tutorial/essential/io/index.html */ import java.net.*; // Hier importieren wir unsere Socket "toolbox" import java.io.*; // Damit können wir Zeichen lesen und ausgeben public class UserAgent21a { public static void main(String[] args) throws Exception { Socket mySocket = new Socket(args[0], 80); /* PrintWriter PrintStream * Contain convenient printing methods. These are the easiest * streams to write to, so you will often see other writable * streams wrapped in one of these. */ PrintWriter out = new PrintWriter (mySocket.getOutputStream(), true); /* InputStreamReader and OutputStreamWriter * A reader and writer pair that forms the bridge between byte streams * and character streams. An InputStreamReader reads bytes from an * InputStream and converts them to characters using either the default * character-encoding or a character-encoding specified by name. * Similarly, an OutputStreamWriter converts characters to bytes using * either the default character-encoding or a character-encoding specified * by name and then writes those bytes to an OutputStream. */ InputStreamReader in = new InputStreamReader(mySocket.getInputStream()); // send request (PrintWriter contains 'println' method) out.println("GET " + args[1] + " HTTP/1.0"); out.println(); // read answer int nextchar; while ((nextchar = in.read()) != -1) { System.out.print((char)nextchar); } out.close(); in.close(); } // of main } // of UserAgent class