05 HttpCLient----Get

时间:2023-1-17    作者:老大夫    分类: 传智JAVA爬虫学习笔记


类文件

package cn.itcast.crawler.test;

import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.io.entity.EntityUtils;

public class HttpGetTest {

    public static void main(String[] args) {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建HttpGet对象,url访问地址
        org.apache.hc.client5.http.classic.methods.HttpGet httpGet = new org.apache.hc.client5.http.classic.methods.HttpGet("http://www.itcast.cn");

        CloseableHttpResponse response = null;
        try {
        //使用HttpClient发起请求,获取response
            response = httpClient.execute(httpGet);
            //解析响应
            if(response.getCode()==200){
                String content = EntityUtils.toString(response.getEntity(), "utf8");
                System.out.println(content.length());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //关闭response
            try {
                response.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //解析响应

    }
}


扫描二维码,在手机上阅读

推荐阅读: