使用HttpClient和HttpRequest发送HTTP请求

项目中经常会用到向第三方系统发送请求来传递数据或者获得信息,一般用的比较多的为HttpClient 和 HttpRequest,这里简要总结一下 HttpClient 和 HttpRequest 的用法

一、HttpClient

  1. 发送get请求
java 复制代码
public static String get(String url, Map<String, String> headMap, int timeout) {
    String result = null;
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    try {
        HttpGet httpGet = new HttpGet(url);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).build();
        httpGet.setConfig(requestConfig);
        setHead(httpGet, headMap);
        response = httpclient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity, "UTF-8");
        EntityUtils.consume(entity);
    } catch (Exception e) {
        LOG.error("HttpGet [{}] error: {}", url, e.getMessage(), e);
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        try {
            if (null != response) {
                response.close();  
            }
            httpclient.close();
        } catch (IOException e) {
            LOG.error("HttpGet [{}] httpclient close error: {}", url, e.getMessage(), e);
        }
    }
    return result;
}
  1. 发送post请求
java 复制代码
public static String postJson(String url, String body, Map<String, String> headMap, int timeout) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    try {
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
        setHead(httpPost, headMap);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).build();
        httpPost.setConfig(requestConfig);
        httpPost.setEntity(new StringEntity(body, "UTF-8"));
        response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String responseContent = EntityUtils.toString(entity, "UTF-8");
        EntityUtils.consume(entity);
        return responseContent;
    } catch (Exception e) {
        LOG.error("HttpPostJson [{}] error: {}", url, e.getMessage(), e);
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        try {
            response.close();
            httpClient.close();
        } catch (IOException e) {
            LOG.error("HttpPostJson [{}] httpclient close error: {}", url, e.getMessage(), e);
        }
    }
}
java 复制代码
private static void setHead(HttpRequestBase httpRequestBase, Map<String, String> headMap) {
    if (headMap != null && headMap.size() > 0) {
        Set<String> keySet = headMap.keySet();
        for (String key : keySet) {
            httpRequestBase.addHeader(key, headMap.get(key));
        }
    }
}

优点:连接池(复用HTTP连接)、重试、代理机制,社区活跃,性能更加优异

缺点:配置和API使用较为复杂

二、HttpRequest

  1. 发送get请求
java 复制代码
public static String get(String methodName, String url, Map<String, Object> paramMap, Map<String, String> headers){
    return HttpRequest.get(url).addHeaders(headers).form(paramMap).execute().body();
}
  1. 发送post请求
java 复制代码
public static String postForm(String methodName, String url, Map<String, Object> paramMap, Map<String, String> headers, String paramStr){
    return HttpRequest.post(url).addHeaders(headers).form(paramMap).body(paramStr).execute().body();
}

优点:功能简单,整体比较易用

缺点:性能一般,社区支持较弱

三、对比

hutool 的 HttpRequest 底层是基于 HttpClient 的,是对其进行了封装,提供了更加简便的API请求,使用 HttpRequest 可在一行代码中实现发送简单的HTTP请求,如果需要使用 HttpClient 更加复杂的操作,则可以通过自定义 HttpClient 并通过 setHttpClient() 方法传入 HttpClient

例如通过自定义Httpclient实现设置最大连接数量

java 复制代码
// 创建连接池管理器
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100); // 设置最大连接数
connectionManager.setDefaultMaxPerRoute(20); // 设置每个路由的最大连接数

// 创建自定义的HttpClient
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
        .setConnectionManager(connectionManager);

// 使用 Hutool 的 HttpRequest 发送请求
HttpResponse response = HttpRequest.get("https://example.com")
        .timeout(5000) // 设置超时时间
        .setHttpClient(httpClientBuilder.build()) // 设置自定义的HttpClient
        .execute();

总结

  1. 小型简单项目可使用 HttpRequest 快速请求,较大型复杂项目适合使用 HttpClient
  2. HttpClient 引入 httpclient 依赖,HttpRequest 引入 hutool 依赖
  3. 如果有连接池、重试、代理等机制的需求则可使用 HttpClient
  4. 高性能,高并发,有复杂Http操作的场景则可使用 HttpClient
相关推荐
初听于你4 小时前
缓存技术揭秘
java·运维·服务器·开发语言·spring·缓存
小蒜学长5 小时前
springboot多功能智能手机阅读APP设计与实现(代码+数据库+LW)
java·spring boot·后端·智能手机
YongCheng_Liang7 小时前
网络工程师笔记8-OSPF协议
运维·网络·网络协议
BossFriday8 小时前
【手撸IM】高性能HTTP API服务设计与实现
网络·网络协议·http
zizisuo8 小时前
解决在使用Lombok时maven install 找不到符号的问题
java·数据库·maven
笨蛋少年派8 小时前
JAVA基础语法
java·开发语言
渡我白衣8 小时前
深入剖析:boost::intrusive_ptr 与 std::shared_ptr 的性能边界和实现哲学
开发语言·c++·spring
Haooog8 小时前
654.最大二叉树(二叉树算法)
java·数据结构·算法·leetcode·二叉树
我真的是大笨蛋8 小时前
依赖倒置原则(DIP)
java·设计模式·性能优化·依赖倒置原则·设计规范
北京耐用通信8 小时前
耐达讯自动化Modbus RTU转Profibus,让电磁阀连接从此与众不同!
网络·人工智能·网络协议·网络安全·自动化