福州建网站 做网页,临沂河东区建设局网站,建设网站教程视频,广州免费技能培训班Java如何使用 HttpClientUtils 发起 HTTP 请求 一、前言1.HttpClientUtils 类概览2.解析 HttpClientUtils 类3.使用 HttpClientUtils 类 一、前言
在现代的软件开发中#xff0c;经常需要与远程服务器进行通信#xff0c;例如获取数据或发送数据。Apache HttpClient 是一个流… 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, ListNameValuePair 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, ListNameValuePair 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, ListNameValuePair 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 类 静态常量和变量: 定义了最大连接数、连接超时时间、Socket 超时时间等常量和变量。 初始化和销毁: 在静态代码块中初始化了 HttpClient 和连接管理器 PoolingHttpClientConnectionManager。使用 ScheduledExecutorService 定期清理过期和空闲连接。在 destroyByJvmExit 方法中注册了一个 JVM 退出钩子确保在 JVM 关闭时关闭 HttpClient。 HTTP 请求方法: postWithParamsForString 方法用于执行带参数的 POST 请求。使用 HttpPost 构建请求设置 URL 和请求参数最终调用 executeRequest 方法执行请求。 执行请求和处理响应: executeRequest 方法接收 HttpUriRequest执行 HTTP 请求并处理响应。检查响应的状态码如果是 200OK则读取响应实体并将其转换为字符串返回。使用日志记录错误和警告信息确保代码的健壮性和可靠性。
3.使用 HttpClientUtils 类
使用 HttpClientUtils 类可以简化 HTTP 请求的编写和管理具体步骤如下
public class Main {public static void main(String[] args) {ListNameValuePair 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 通信同时通过连接池和定期清理机制提升性能和稳定性。