diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/ApacheHttpClientConfig.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/ApacheHttpClientConfig.java index f90ea01a7..061c2d5fc 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/ApacheHttpClientConfig.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/ApacheHttpClientConfig.java @@ -41,11 +41,6 @@ public class ApacheHttpClientConfig extends ClientConfig { * 每个路由的最大连接数 */ private int maxPerRoute; - /** - * 重定向最大次数 - */ - private int maxRedirects; - /** * 获取最大连接总数 * @@ -85,24 +80,4 @@ public class ApacheHttpClientConfig extends ClientConfig { this.maxPerRoute = maxPerRoute; return this; } - - /** - * 获取重定向最大次数 - * - * @return 重定向最大次数 - */ - public int getMaxRedirects() { - return maxRedirects; - } - - /** - * 设置重定向最大次数 - * - * @param maxRedirects 重定向最大次数 - * @return this - */ - public ApacheHttpClientConfig setMaxRedirects(final int maxRedirects) { - this.maxRedirects = maxRedirects; - return this; - } } diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/ClientConfig.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/ClientConfig.java index d029ea748..5e21fe6db 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/ClientConfig.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/ClientConfig.java @@ -40,6 +40,10 @@ public class ClientConfig { * 默认连接超时 */ private int connectionTimeout; + /** + * 默认连接保持时间(连接池中),单位:毫秒 + */ + private long timeToLive; /** * 默认读取超时 */ @@ -112,6 +116,26 @@ public class ClientConfig { return this; } + /** + * 获取连接保持时间(连接池中),单位:毫秒 + * + * @return 连接保持时间(连接池中),单位:毫秒 + */ + public long getTimeToLive() { + return timeToLive; + } + + /** + * 设置连接保持时间(连接池中),单位:毫秒 + * + * @param timeToLive 连接保持时间(连接池中),单位:毫秒 + * @return this + */ + public ClientConfig setTimeToLive(final long timeToLive) { + this.timeToLive = timeToLive; + return this; + } + /** * 获取读取超时,单位:毫秒 * diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient4/HttpClient4Engine.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient4/HttpClient4Engine.java index ac3a13a9c..9d95aeabe 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient4/HttpClient4Engine.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient4/HttpClient4Engine.java @@ -50,6 +50,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; /** * Apache HttpClient5的HTTP请求引擎 @@ -154,9 +155,18 @@ public class HttpClient4Engine extends AbstractClientEngine { * @return PoolingHttpClientConnectionManager */ private PoolingHttpClientConnectionManager buildConnectionManager(final ClientConfig config) { + long timeToLive = config.getTimeToLive(); + if(timeToLive <= 0){ + timeToLive = -1; + } final PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager( // SSL配置,当config.getSslInfo()为null时,使用默认配置 - ConnectionSocketFactoryRegistryBuilder.build(config.getSslInfo()) + ConnectionSocketFactoryRegistryBuilder.build(config.getSslInfo()), + null, + null, + null, + timeToLive, + TimeUnit.MILLISECONDS ); // 连接池配置 @@ -195,9 +205,6 @@ public class HttpClient4Engine extends AbstractClientEngine { if (readTimeout > 0) { requestConfigBuilder.setSocketTimeout(readTimeout); } - if (config instanceof ApacheHttpClientConfig) { - requestConfigBuilder.setMaxRedirects(((ApacheHttpClientConfig) config).getMaxRedirects()); - } return requestConfigBuilder.build(); } diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient5/HttpClient5Engine.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient5/HttpClient5Engine.java index da3127bc2..61d987a2f 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient5/HttpClient5Engine.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/httpclient5/HttpClient5Engine.java @@ -173,13 +173,20 @@ public class HttpClient5Engine extends AbstractClientEngine { .build() ); } - // 连接超时配置 + + // 默认连接配置 + final ConnectionConfig.Builder connectionConfigBuilder = ConnectionConfig.custom(); final int connectionTimeout = config.getConnectionTimeout(); if (connectionTimeout > 0) { - connectionManagerBuilder.setDefaultConnectionConfig(ConnectionConfig.custom() + connectionConfigBuilder .setSocketTimeout(connectionTimeout, TimeUnit.MILLISECONDS) - .setConnectTimeout(connectionTimeout, TimeUnit.MILLISECONDS).build()); + .setConnectTimeout(connectionTimeout, TimeUnit.MILLISECONDS); } + final long timeToLive = config.getTimeToLive(); + if(timeToLive > 0){ + connectionConfigBuilder.setTimeToLive(timeToLive, TimeUnit.MILLISECONDS); + } + connectionManagerBuilder.setDefaultConnectionConfig(connectionConfigBuilder.build()); // 连接池配置 if (config instanceof ApacheHttpClientConfig) { @@ -215,9 +222,6 @@ public class HttpClient5Engine extends AbstractClientEngine { if (readTimeout > 0) { requestConfigBuilder.setResponseTimeout(readTimeout, TimeUnit.MILLISECONDS); } - if (config instanceof ApacheHttpClientConfig) { - requestConfigBuilder.setMaxRedirects(((ApacheHttpClientConfig) config).getMaxRedirects()); - } return requestConfigBuilder.build(); } diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/okhttp/OkHttpClientConfig.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/okhttp/OkHttpClientConfig.java index 2202bd8ad..43fe60789 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/okhttp/OkHttpClientConfig.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/okhttp/OkHttpClientConfig.java @@ -16,7 +16,6 @@ package org.dromara.hutool.http.client.engine.okhttp; -import okhttp3.ConnectionPool; import org.dromara.hutool.http.client.ClientConfig; /** @@ -35,25 +34,28 @@ public class OkHttpClientConfig extends ClientConfig { return new OkHttpClientConfig(); } - private ConnectionPool connectionPool; + /** + * 默认最大空闲连接数 + */ + private int maxIdle; /** - * 获取连接池 + * 获取最大空闲连接数 * - * @return ConnectionPool + * @return 最大空闲连接数 */ - public ConnectionPool getConnectionPool() { - return connectionPool; + public int getMaxIdle() { + return maxIdle; } /** - * 设置连接池 + * 设置最大空闲连接数 * - * @param connectionPool ConnectionPool + * @param maxIdle 最大空闲连接数 * @return this */ - public OkHttpClientConfig setConnectionPool(final ConnectionPool connectionPool) { - this.connectionPool = connectionPool; + public ClientConfig setMaxIdle(final int maxIdle) { + this.maxIdle = maxIdle; return this; } } diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/okhttp/OkHttpEngine.java b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/okhttp/OkHttpEngine.java index d4eeffab5..914cfd5db 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/okhttp/OkHttpEngine.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/client/engine/okhttp/OkHttpEngine.java @@ -16,6 +16,7 @@ package org.dromara.hutool.http.client.engine.okhttp; +import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.util.ObjUtil; @@ -120,9 +121,18 @@ public class OkHttpEngine extends AbstractClientEngine { } // 连接池 - if (config instanceof OkHttpClientConfig) { - builder.connectionPool(((OkHttpClientConfig) config).getConnectionPool()); + int maxIdle = 0; + if(config instanceof OkHttpClientConfig){ + maxIdle = ((OkHttpClientConfig) config).getMaxIdle(); } + if(maxIdle <= 0){ + maxIdle = 5; + } + long timeToLive = config.getTimeToLive(); + if(timeToLive <= 0){ + timeToLive = TimeUnit.MINUTES.toMillis(5); + } + builder.connectionPool(new ConnectionPool(maxIdle, timeToLive, TimeUnit.MILLISECONDS)); // 关闭自动重定向,手动实现 builder.followRedirects(false); diff --git a/hutool-http/src/test/java/org/dromara/hutool/http/client/DownloadTest.java b/hutool-http/src/test/java/org/dromara/hutool/http/client/DownloadTest.java index 12c39476d..2f7265eeb 100644 --- a/hutool-http/src/test/java/org/dromara/hutool/http/client/DownloadTest.java +++ b/hutool-http/src/test/java/org/dromara/hutool/http/client/DownloadTest.java @@ -52,6 +52,7 @@ public class DownloadTest { } @Test + @Disabled void downloadWithHeaderTest() { HttpDownloader.of("https://hutool.cn/") .header(MapUtil.of("Authorization", "token"))