HttpClient请求返回结果对象HttpEntity处理

HttpClient请求返回结果对象HttpEntity处理转成字符串一串图片说明

2个回答

多次使用了EntityUtils.toString了

l135033303
景略 我也这问题,尴尬
3 个月之前 回复

class VarietyUtil1 implements Runnable {

public static Variety getHotVariety() {
    String url = "http://top.baidu.com/buzz?b=19&c=3&fr=topcategory_c3";
    String html = pickData(url);
    Variety v = analyzeHTMLByString(html);
    ConnectionMongo cm = new ConnectionMongo();
    cm.insertVariety(v);
    return v;
}

/*
 * 爬取网页信息
 */
private static String pickData(String url) {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {
        HttpGet httpget = new HttpGet(url);
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            // 获取响应实体
            HttpEntity entity = response.getEntity();
            // 打印响应状态
            if (entity != null) {
                InputStream in = entity.getContent();
                // byte[] b=new byte[in.available()];
                // in.read(b);
                BufferedReader br = new BufferedReader(new InputStreamReader(in, "gbk"));
                String temp = "";
                String s = "";
                while ((temp = br.readLine()) != null) {
                    s = s + temp;
                }
                return s;
            } else {
                String content = "热门综艺节目抓取失败,请检查";
                ErrorLog el = new ErrorLog();
                Remind remind = new Remind();
                remind.remind(el.getVerietyLog(), content);
                return null;
            }
        } finally {
            response.close();
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // 关闭连接,释放资源
        try {
            httpclient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

/*
 * 使用jsoup解析网页信息
 */
private static Variety analyzeHTMLByString(String html) {
    Variety v = new Variety();
    String[] arr = new String[3];
    Document document = Jsoup.parse(html);
    // document.select("meta").attr("charset", "utf-8");
    // System.out.println(document);
    Elements array = document.getElementsByClass("keyword");
    System.out.println(array.size());
    String content = "热门综艺节目抓取失败,请检查";
    ErrorLog el = new ErrorLog();
    if (array.size() == 0) {
        Remind remind = new Remind();
        remind.remind(el.getVerietyLog(), content);
        return null;
    }else{
        if (array.size() >= 3) {
            for (int i = 0; i < 3; i++) {
                String name = array.get(i).child(0).text();
                arr[i] = name;
            }
        } else {
            for (int i = 0; i < array.size(); i++) {
                String name = array.get(i).child(0).text();
                arr[i] = name;
            }
        }
        v.setHot1(arr[0]);
        v.setHot2(arr[1]);
        v.setHot3(arr[2]);
        return v;
    }

}

@Override
public void run() {
    String url = "http://top.baidu.com/buzz?b=19&c=3&fr=topcategory_c3";
    String html = pickData(url);
    Variety v = analyzeHTMLByString(html);
    ConnectionMongo cm = new ConnectionMongo();
    java.util.Calendar c = java.util.Calendar.getInstance();
    v.setAddTime(c.getTime());
    cm.insertVariety(v);
    System.out.println(v);
}

}

public class VarietyUtil {
public static void main(String[] args) {
VarietyUtil1 t = new VarietyUtil1();
DateSwitch ds = new DateSwitch();
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
// 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
service.scheduleAtFixedRate(t, ds.HowOfen(), 86400, TimeUnit.SECONDS);
}
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问