当前位置: 首页> 健康> 养生 > Java如何使用 HttpClientUtils 发起 HTTP 请求

Java如何使用 HttpClientUtils 发起 HTTP 请求

时间:2025/7/10 8:31:12来源:https://blog.csdn.net/weixin_46146718/article/details/140314600 浏览次数:0次

Java如何使用 HttpClientUtils 发起 HTTP 请求

  • 一、前言
    • 1.HttpClientUtils 类概览
    • 2.解析 HttpClientUtils 类
    • 3.使用 HttpClientUtils 类


一、前言

在现代的软件开发中,经常需要与远程服务器进行通信,例如获取数据或发送数据。Apache HttpClient 是一个流行的 Java HTTP 客户端库,能够简化 HTTP 请求的发起和响应处理。本文将介绍如何使用自定义的 HttpClientUtils 类来利用 Apache HttpClient 发起 POST 请求,并且管理连接池以优化性能。

1.HttpClientUtils 类概览

public class HttpClientUtils {// 静态常量和变量声明private static final int MAX_TOTAL_CONN = 600;private static final int MAX_CONN_PER_HOST = 300;private static final int SOCKET_TIMEOUT = 5000;private static final int CONNECTION_TIMEOUT = 200;private static final int CONNECTION_MANAGER_TIMEOUT = 100;private static CloseableHttpClient httpclient;private static PoolingHttpClientConnectionManager connMrg;private static String encoding = StandardCharsets.UTF_8.name();private static Logger log = LoggerFactory.getLogger(HttpClientUtils.class);private static final ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool(2);// 静态代码块,用于初始化 HttpClient 和连接管理器,并设置 JVM 退出时关闭 HttpClientstatic {init();destroyByJvmExit();}// 初始化连接管理器和 HttpClientprivate static void init() {connMrg = new PoolingHttpClientConnectionManager();connMrg.setMaxTotal(MAX_TOTAL_CONN);connMrg.setDefaultMaxPerRoute(MAX_CONN_PER_HOST);httpclient = HttpClients.custom().setConnectionManager(connMrg).setDefaultRequestConfig(HttpClientUtils.defaultRequestConfig()).build();// 定时任务,定期清理过期和空闲连接scheduledService.scheduleAtFixedRate(() -> {connMrg.closeExpiredConnections();connMrg.closeIdleConnections(CONNECTION_MANAGER_TIMEOUT, TimeUnit.MILLISECONDS);}, 0, CONNECTION_MANAGER_TIMEOUT, TimeUnit.MILLISECONDS);}// JVM 退出时关闭 HttpClientprivate static void destroyByJvmExit() {Runtime.getRuntime().addShutdownHook(new Thread(() -> {try {httpclient.close();} catch (IOException e) {log.error("Error closing HttpClient: {}", e.getMessage());}}));}// 创建 HttpClientContextprivate static HttpClientContext createContext() {return HttpClientContext.create();}// 创建默认的 RequestConfigprivate static RequestConfig defaultRequestConfig() {return RequestConfig.custom().setConnectTimeout(CONNECTION_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).setConnectionRequestTimeout(CONNECTION_MANAGER_TIMEOUT).build();}// 发起带参数的 POST 表单请求,返回字符串结果public static String postWithParamsForString(String url, List<NameValuePair> params) {HttpPost httpPost = new HttpPost();try {URI uri = new URIBuilder(url).build();httpPost.setURI(uri);httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));return executeRequest(httpPost);} catch (URISyntaxException | IOException e) {log.error("Error executing POST request: {}", e.getMessage());} finally {httpPost.releaseConnection();}return null;}// 发起 GET 请求,返回字符串结果public static String get(String url, List<NameValuePair> params) {HttpGet httpGet = new HttpGet();try {URI uri = new URIBuilder(url).setParameters(params).build();httpGet.setURI(uri);return executeRequest(httpGet);} catch (URISyntaxException | IOException e) {log.error("HTTP GET request failed", e);} finally {httpGet.releaseConnection();}return null;}// 发起 Post 请求,返回字符串结果public static String post(String url, List<NameValuePair> params) {HttpPost httpPost = new HttpPost();try {httpPost.setURI(new URI(url));httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));return executeRequest(httpPost);} catch (URISyntaxException | IOException e) {log.error("HTTP POST request failed", e);} finally {httpPost.releaseConnection();}return null;}// 执行 HTTP 请求并处理响应private static String executeRequest(HttpUriRequest request) throws IOException {try (CloseableHttpResponse response = httpclient.execute(request, createContext())) {int statusCode = response.getStatusLine().getStatusCode();if (statusCode == HttpStatus.SC_OK) {HttpEntity entity = response.getEntity();if (entity != null) {return EntityUtils.toString(entity, encoding);} else {log.warn("Empty response entity");}} else {log.error("HTTP request failed with status code: {}", statusCode);}} catch (IOException e) {log.error("HTTP request execution failed: {}", e.getMessage());throw e;}return null;}
}

2.解析 HttpClientUtils 类

  1. 静态常量和变量:

    • 定义了最大连接数、连接超时时间、Socket 超时时间等常量和变量。
  2. 初始化和销毁:

    • 在静态代码块中初始化了 HttpClient 和连接管理器 PoolingHttpClientConnectionManager
    • 使用 ScheduledExecutorService 定期清理过期和空闲连接。
    • destroyByJvmExit 方法中,注册了一个 JVM 退出钩子,确保在 JVM 关闭时关闭 HttpClient
  3. HTTP 请求方法:

    • postWithParamsForString 方法用于执行带参数的 POST 请求。
    • 使用 HttpPost 构建请求,设置 URL 和请求参数,最终调用 executeRequest 方法执行请求。
  4. 执行请求和处理响应:

    • executeRequest 方法接收 HttpUriRequest,执行 HTTP 请求并处理响应。
    • 检查响应的状态码,如果是 200(OK),则读取响应实体并将其转换为字符串返回。
    • 使用日志记录错误和警告信息,确保代码的健壮性和可靠性。

3.使用 HttpClientUtils 类

使用 HttpClientUtils 类可以简化 HTTP 请求的编写和管理,具体步骤如下:

public class Main {public static void main(String[] args) {List<NameValuePair> params = new ArrayList<>();params.add(new BasicNameValuePair("param1", "value1"));params.add(new BasicNameValuePair("param2", "value2"));String response = HttpClientUtils.postWithParamsForString("http://example.com/api", params);if (response != null) {System.out.println("Response: " + response);} else {System.err.println("Failed to execute POST request");}}
}

通过以上学习,你现在应该了解如何使用 HttpClientUtils 类来管理 HTTP 客户端和发起请求。这种方式可以帮助你在 Java 应用中更高效地处理 HTTP 通信,同时通过连接池和定期清理机制提升性能和稳定性。

关键字:Java如何使用 HttpClientUtils 发起 HTTP 请求

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: