我有HttpClient 4.1。请看下面的程序:简单的java HttpClient程序不能正常工作
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
。什么可能是错的?
它究竟在哪里抛出异常?你的代码适合我。另外,运行时/进程代码如何与您的问题相关联,如果它不是请删除。 –
尝试使用e.printStackTrace()而不是System.out.println(e)来获取完整的堆栈跟踪。此外,您的reps.getHeaders(s)返回一个数组,s不会更改。执行后立即执行 –
。它仅适用于google.com。我试过www.affinity.com,它工作。怎么来的 !! – Shades88