Java如何使用 HttpClientUtils 发起 HTTP 请求
一、前言
在现代的软件开发中,经常需要与远程服务器进行通信,例如获取数据或发送数据。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 退出时关闭 HttpClient
static {
init();
destroyByJvmExit();
}
// 初始化连接管理器和 HttpClient
private 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 退出时关闭 HttpClient
private static void destroyByJvmExit() {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
httpclient.close();
} catch (IOException e) {
log.error("Error closing HttpClient: {}", e.getMessage());
}
}));
}
// 创建 HttpClientContext
private static HttpClientContext createContext() {
return HttpClientContext.create();
}
// 创建默认的 RequestConfig
private 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, String json) {
HttpPost httpPost = new HttpPost();
try {
httpPost.setURI(new URI(url));
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(new StringEntity(json));
return executeRequest(httpPost);
} catch (UnsupportedEncodingException e) {
log.error("Unsupported encoding for JSON entity", e);
} 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 请求并处理响应。- 检查响应的状态码,如果是 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 通信,同时通过连接池和定期清理机制提升性能和稳定性。