当前位置: 首页 > article >正文

HttpClient快速入门

HttpClient-基本使用

httpGet和httpPost

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;


public class HttpClientTest {

    @Test
    public void testGet() throws IOException {
        //1.需要HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2.创建HttpGet请求并进行相关设置
        HttpGet httpGet = new HttpGet("https://www.baidu.com/");
        httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");
        //3.发起请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //4.获取响应数据
        if (response.getStatusLine().getStatusCode() == 200){ //200表示响应成功
            String html = EntityUtils.toString(response.getEntity(), "UTF-8");
            System.out.println(html);
        }
        //5.关闭资源
        httpClient.close();
        response.close();
    }

    @Test
    public void testPost() throws IOException {
        //1.需要HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2.创建HttpPost请求并进行相关设置
        HttpPost httpPost = new HttpPost("https://www.itcast.cn/");
        //准备集合用来存放参数
        ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("username","donglin"));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
        httpPost.setEntity(entity);
        httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");
        //3.发起请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        //4.获取响应数据
        if (response.getStatusLine().getStatusCode() == 200){ //200表示响应成功
            String html = EntityUtils.toString(response.getEntity(), "UTF-8");
            System.out.println(html);
        }
        //5.关闭资源
        httpClient.close();
        response.close();
    }
}

HttpClient-连接池

    @Test
    public void testPool() throws IOException {
        //1.创建HttpClient连接管理器
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        //2.设置参数
        cm.setMaxTotal(200);  //设置最大连接数
        cm.setDefaultMaxPerRoute(20); //设置每个主机的最大并发
        //调用两次进行试验
        doGet(cm);
        doGet(cm);
    }

    private void doGet(PoolingHttpClientConnectionManager cm) throws IOException {
        //3.从连接池获取HttpClient对象
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
        //4.创建HttpGet对象
        HttpGet httpGet = new HttpGet("https://www.itcast.cn/");
        //5.发送请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //6.获取数据
        if (response.getStatusLine().getStatusCode() == 200){ //200表示响应成功
            String html = EntityUtils.toString(response.getEntity(), "UTF-8");
            System.out.println(html);
        }
        //7.关闭资源
        response.close();
        //httpClient.close();  //这里不用关闭,因为是连接池

    }

HttpClient-超时设置 & 添加代理

    @Test
    public void testConfig() throws IOException {
        //0.创建请求配置对象
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(10000) //设置连接超时时间
                .setConnectTimeout(10000) //设置创建连接超时时间
                .setConnectionRequestTimeout(10000) //设置请求超时时间
                .setProxy(new HttpHost("111.177.63.86",8888))
                .build();
        //1.创建HttpClient对象
        //CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
        //2.创建HttpGet请求并进行相关设置
        HttpGet httpGet = new HttpGet("https://www.itcast.cn/");
        //3.发起请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //4.获取响应数据
        if (response.getStatusLine().getStatusCode() == 200){ //200表示响应成功
            String html = EntityUtils.toString(response.getEntity(), "UTF-8");
            System.out.println(html);
        }
        //5.关闭资源
        httpClient.close();
        response.close();
    }

HttpClient-封装

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import sun.net.www.http.HttpClient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public abstract class HttpUtils {

    private static PoolingHttpClientConnectionManager cm; //声明httpClient管理器对象(连接处)
    private static List<String> userAgentList = null;
    private static RequestConfig config = null;

    //静态代码块会在类被夹在的时候执行
    static {
        cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(200);
        cm.setDefaultMaxPerRoute(20);
        config = RequestConfig.custom()
                .setSocketTimeout(10000) //设置连接超时时间
                .setConnectTimeout(10000) //设置创建连接超时时间
                .setConnectionRequestTimeout(10000) //设置请求超时时间
                .setProxy(new HttpHost("111.177.63.86",8888))
                .build();
        userAgentList = new ArrayList<String>();
        userAgentList.add("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36");
        userAgentList.add("Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36");
        userAgentList.add("Mozilla/5.01 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36");
        userAgentList.add("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Shuame)");
        userAgentList.add("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.3.8126.400)");
        userAgentList.add("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; .NET CLR 1.1.4322)");
        userAgentList.add("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.3)");
        userAgentList.add("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)");
        userAgentList.add("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0");
    }

    public static String getHtml(String url){
        //1.从连接池中获取httpClient对象
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
        //2.创建HttpGet对象
        HttpGet httpGet = new HttpGet(url);
        //3.设置请求配置对象和请求头
        httpGet.setConfig(config);
        httpGet.setHeader("User-Agent",userAgentList.get(new Random().nextInt(userAgentList.size())));
        //4.发起请求
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            //5.获取响应内容
            if (response.getStatusLine().getStatusCode() == 200){ //200表示响应成功
                String html = "";
                if (response.getEntity()!=null){
                    html = EntityUtils.toString(response.getEntity(), "UTF-8");
                    System.out.println(html);
                }
                return html;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                response.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return null;
    }

    public static void main(String[] args) {
        HttpUtils.getHtml("https://www.itcast.cn/");
    }

}

http://www.kler.cn/a/105206.html

相关文章:

  • selenium模拟登录无反应
  • 电商时代,VR全景如何解决实体店难做没流量?
  • 输入/输出应用程序接口和设备驱动程序接口
  • Java学习笔记(三)
  • 使用VisualStudio生成类图结构图for高效阅读代码
  • 【LeetCode】881 救生艇 中等题
  • OPC UA:工业领域的“HTML”
  • kafka3.X基本概念和使用
  • LeetCode 面试题 16.01. 交换数字
  • Linux C语言开发-D9输入输出
  • 【2024秋招】万得后端开发java 2023-7-13 2.30pm 一二面面经(附答案)
  • FreeRTOS学习2018.6.27
  • Kafka - 异步/同步发送API
  • 使用spring boot的程序主线程中异步访问外部接口
  • Python---练习:使用for循环嵌套实现打印九九乘法表
  • 【异常】理解Java中的异常处理机制
  • openCV的CUDA GPU 版本安装 (Ubuntu windows 通用)
  • AMD大规模裁员15%? 赔偿N+7?官方回应来了 | 百能云芯
  • Java零基础入门-赋值运算符
  • 【会议征稿通知】2024第四届神经网络、信息与通信工程国际学术会议(NNICE 2024)