0

我有 HttpClient 4.1。请看以下程序:

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);
        }
    }
}

当我运行它时,我得到org.apache.http.client.ClientProtocolException. 有什么问题?

4

2 回答 2

0

您是否考虑过使用 HttpURLConnection 而不是 HttpClient?

于 2012-06-25T20:10:38.390 回答
0

谷歌可能会将您重定向到您的“本地”谷歌网站。我住在荷兰,当我访问 www.google.com 时,它会响应 HTTP 302 重定向到 www.google.nl。

我不确定默认 http 客户端是如何配置的,但默认情况下它可能不遵循重定向。

于 2012-06-25T20:30:43.910 回答