[英]Simple java HttpClient program not working
我有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
。 有什么事嗎
您是否考慮過使用HttpURLConnection代替HttpClient?
Google可能會將您重定向到您的“本地” Google網站。 我住在荷蘭,當我到達www.google.com時,它會通過HTTP 302重定向到www.google.nl。
我不確定默認http客戶端的配置方式,但是默認情況下它可能不遵循重定向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.