I'm attempting to write a simple Java http client that simply prints out one line of the server response. My problem is that I get no response from the server. Here is what I have, which is compiling and running with no explicit errors, it just hangs after I type a hostname, e.g. 'www.google.com':
import java.io.*;
import java.net.*;
public class DNSTest {
// Constructor
public DNSTest() { }
// Builds GET request, opens socket, waits for response, closes
public static void main(String[] args) throws Exception{
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//For each hostname
while ((line = br.readLine()) != null){
//Resolve the hostname to an IP address
InetAddress ip = InetAddress.getByName(line);
//Open socket on ip address
Socket socket = new Socket(ip, 80);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//Send request
out.println("GET /index.html HTTP/1.0\n");
//Read one line of input
System.out.println("Response from "+line+": "+in.readLine());
}
}
}
Any suggestions? Note that this assumes an 'index.html' exists - it still just hangs even if this is true.