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, eg '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.
I think I have been able to reproduce the problem by applying a small change to the code, so now it doesn't work on my machine and exhibits the same behavior as it does in your environment. I just changed the out.println
call to a simpler out.print
, and lo and behold, the program hangs after sending out the request but before the last println
call.
As far as I can remember, HTTP requests need to supply an empty line after the headers, where the line separator must be the \\r\\n
couple of characters. I suppose that your environment is such that println
does not send the correct line separators (you can check the value of System.getProperty("line.separator")
to verify which are the ones used on your system), so the request is interpreted by the server as incomplete and you get nothing on the input side. Some servers are quite forgiving and accept just \\n
as line separator, but if you happen to send \\n
explicitly and \\r\\n
implicitly (by means of println
) then your supposedly empty line contains the \\r
character and is no more seen as empty, possibly faulting the request to the effect of preventing the server to send back any response.
Therefore, my advice is to use print
and send the correct line separators explicitly. Also, you very probably need to add a call to flush
, maybe because there's an empty line in between the output - I don't really know, but without invoking flush
my program still hangs. So, to sum up, the following should work:
// send request
out.print("GET /index.html HTTP/1.0\r\n\r\n");
out.flush();
// read one line of input
System.out.println("Response from " + line + ": " + in.readLine());
At least, I can confirm the program works on my machine with these changes, too.
I recommend to make
out.flush();
after out.println(...);
This will send the data to the remote server. It looks like the data never leaves your local buffer.
Ie
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");
out.flush();
//Read one line of input
System.out.println("Response from "+line+": "+in.readLine());
And if I type google.com it gives me:
Response from google.com: HTTP/1.0 200 OK
If it doesn't work, try this:
HttpURLConnection connection = (HttpURLConnection)
new URL(url).openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setDoOutput(false);
connection.setUseCaches(false);
connection.setRequestProperty("Accept", "text/html");
final InputStream is = connection.getInputStream();
// construct BufferredStreamReader from the is
Note, the URL should be like "http://google.com" not just "google.com"
I had exactly the same behavior when i tried the code of the 1st post (on Windows 8). Same thing with all the others ways suggested in this topic.
Then I unninstalled my Antivirus (avast) and everything worked... :$
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.