用HttpClient获取网页内容,用浏览器可以打开百度,但ping不通,求解决方案,

LQF林其锋 发布于 2013/11/18 11:12
阅读 1K+
收藏 0

用HttpClient获取网页内容,用浏览器可以打开百度,但ping不通,求解决方案,昨天不会报这个异常,今天就报了

开发环境JDK1.60 eclipse

代码

01 import java.io.BufferedReader;
02 import java.io.IOException;
03 import java.io.InputStreamReader;
04  
05 import org.apache.http.HttpEntity;
06 import org.apache.http.HttpHost;
07 import org.apache.http.HttpResponse;
08 import org.apache.http.client.ClientProtocolException;
09 import org.apache.http.client.HttpClient;
10 import org.apache.http.client.methods.HttpGet;
11 import org.apache.http.conn.params.ConnRouteParams;
12 import org.apache.http.impl.client.DefaultHttpClient;
13  
14  
15 public class Test {
16  public static void main(String[] args) {
17   String str = "";
18   HttpClient client = new DefaultHttpClient();
19   HttpGet get = new HttpGet("http://www.baidu.com/");
20   try {
21    HttpResponse response = client.execute(get);
22    HttpEntity entity = response.getEntity();
23    BufferedReader reader = new BufferedReader(
24      new InputStreamReader(
25        entity.getContent()));
26    while((str = reader.readLine()) != null) {
27     System.out.println(str);
28    }
29   } catch (ClientProtocolException e) {
30    e.printStackTrace();
31   } catch (IOException e) {
32    e.printStackTrace();
33   }
34  }
35 }
异常
01 java.net.UnknownHostException: www.baidu.com
02  at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
03  at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:850)
04  at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1201)
05  at java.net.InetAddress.getAllByName0(InetAddress.java:1154)
06  at java.net.InetAddress.getAllByName(InetAddress.java:1084)
07  at java.net.InetAddress.getAllByName(InetAddress.java:1020)
08  at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:242)
09  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:130)
10  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:150)
11  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
12  at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:575)
13  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
14  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
15  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
16  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
17  at Test.main(Test.java:22)
加载中
返回顶部
顶部