您现在的位置:首页 >> 前端 >> 内容

httpclient学习抓取网页

时间:2017/6/30 9:27:46 点击:

  核心提示:private final static CloseableHttpClient httpClient = HttpClients.createDefault();//模拟客户端,全局静态常量//抓取...

 

private final static CloseableHttpClient httpClient = HttpClients.createDefault();//模拟客户端,全局静态常量

//抓取网页
public static void testGetUrl(String url)throws IOException{
    HttpGet httpGet = new HttpGet(url);//请求方法
    CloseableHttpResponse httpResponse = httpClient.execute(httpGet);//客户端发生请求,并返回响应
    System.out.println(httpResponse.getStatusLine());//输出响应状态码
    HttpEntity entity = httpResponse.getEntity();//得到响应实体
    dump(entity);//抓取网页内容
}

 

 

private static void dump(HttpEntity entity){
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(entity.getContent(), "utf-8"));
        String str = null;
        while ((str = br.readLine()) != null){
            System.out.println(str);
        }
    }catch (IOException e){
        e.printStackTrace();
    }finally {
        try{
            if(br != null)
                br.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

 

Tags:HT TT TP PC 
作者:网络 来源:xiaoyu_bug