I have HttpClient 4.1. Please have a look at following program:
import org.apache.http.client.methods.*;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
public class SysCommands {
public static void main(String [] args){
try{
HttpClient c = new DefaultHttpClient();
System.out.println("Initial part");
HttpGet method = new HttpGet("http://www.google.com");
HttpResponse resp = c.execute(method);
System.out.println("Method executed");
String s = "";
resp.getHeaders(s);
System.out.println("headers are "+s);
BufferedReader rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
}catch(Exception e){
System.out.println(e);
}
}
}
When I run this, I get org.apache.http.client.ClientProtocolException. What could be wrong?
It might be that google redirects you to your “local” google site. I live in the Netherlands, and when I get http://www.google.com, it responds with a HTTP 302 redirect to http://www.google.nl.
I’m not sure how the default http client is configured, but it is possible that it does not follow redirects by default.