import java.io.*; import java.net.*; public class WebClient0 { public static void main(String[] args) throws IOException { String serverHost = "www.hint.no"; int serverPort = 80; Socket webSocket = null; PrintWriter toSocket = null; BufferedReader fromSocket = null; try { webSocket = new Socket(serverHost, serverPort); toSocket = new PrintWriter(webSocket.getOutputStream(), true); fromSocket = new BufferedReader(new InputStreamReader(webSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: " + serverHost); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: " + serverHost); System.exit(1); } // Sending command: toSocket.println("GET / HTTP/1.0"); // Sending headers: toSocket.println("Accept: text/html, text/plain"); toSocket.println("User-Agent: MMT260WebClient/0.0.1"); toSocket.println("Host: " + serverHost); // Sending empty line: toSocket.println(""); // Receiving header: String serverMessage = fromSocket.readLine(); System.out.println("Header message from WebServer: " + serverMessage); while(!serverMessage.equals("")) { serverMessage = fromSocket.readLine(); System.out.println("Header message from WebServer: " + serverMessage); } // Receiving document: serverMessage = fromSocket.readLine(); while(serverMessage != null){ System.out.println("Document message from WebServer: " + serverMessage); serverMessage = fromSocket.readLine(); } toSocket.close(); fromSocket.close(); webSocket.close(); } }