10 HttpClient-----请求参数

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


类文件

可以给请求设置多种参数,例如创建连接的最长时间,获取连接的最长时间,设置数据传输的最长时间

package cn.itcast.crawler.test;

import jdk.nashorn.internal.ir.RuntimeNode;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.config.RequestConfig;
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;
import org.apache.hc.core5.util.Timeout;

public class HttpConfigTest {

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

        //配置请求信息
        RequestConfig config= RequestConfig.custom().setConnectTimeout(Timeout.ofMilliseconds(100))  //创建连接的最长时间 单位毫秒
                .setConnectionRequestTimeout(Timeout.ofMilliseconds(100)) //获取连接的最长时间
                .setCookieSpec("10*1000") //设置数据传输的最长时间,单位是毫秒
                .build();

        //给请求设置请求信息
        httpGet.setConfig(config);

        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();
            }
        }
        //解析响应

    }
}


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

推荐阅读: