Here's the updated version using Apache HttpComponents Client 5.x with HTTP/2 support and built-in retry handling:
groovy Copy code
package com.i3.net
import org.apache.hc.client5.http.classic.methods.HttpPost
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient
import org.apache.hc.client5.http.impl.classic.HttpClients
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy
import org.apache.hc.core5.http.ClassicHttpResponse
import org.apache.hc.core5.http.ContentType
import org.apache.hc.core5.http.HttpEntity
import org.apache.hc.core5.http.HttpVersion
import org.apache.hc.core5.http.io.entity.EntityUtils
import org.apache.hc.core5.http.io.entity.StringEntity
import org.apache.hc.core5.util.TimeValue
import org.apache.hc.core5.util.Timeout
import java.util.concurrent.TimeUnit
class Http3Service {
static final int MAX_CONCURRENT_CALLS = 200
static CloseableHttpClient client = createClient()
private static CloseableHttpClient createClient() {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager()
cm.setMaxTotal(MAX_CONCURRENT_CALLS)
cm.setDefaultMaxPerRoute(MAX_CONCURRENT_CALLS)
return HttpClients.custom()
.setConnectionManager(cm)
.setDefaultRequestConfig(
org.apache.hc.client5.http.config.RequestConfig.custom()
.setConnectTimeout(Timeout.of(2, TimeUnit.MINUTES))
.setResponseTimeout(Timeout.of(10, TimeUnit.MINUTES))
.build()
)
.setRetryStrategy(new DefaultHttpRequestRetryStrategy(3, TimeValue.ofSeconds(5)))
.setVersionPolicy(org.apache.hc.core5.http.HttpVersionPolicy.FORCE_HTTP_2)
.build()
}
static Map postForm(String url, Map<String, String> formData, Map<String, Map> fileData = [:]) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create()
formData.each { key, value ->
builder.addTextBody(key, value, ContentType.TEXT_PLAIN)
}
fileData.each { key, fileValue ->
builder.addBinaryBody(
key,
fileValue.inputStream,
ContentType.APPLICATION_OCTET_STREAM,
fileValue.filename
)
}
HttpPost httpPost = new HttpPost(url)
httpPost.setEntity(builder.build())
httpPost.setVersion(HttpVersion.HTTP_2)
return executeRequest(httpPost)
}
static Map postJson(String url, String json) {
HttpPost httpPost = new HttpPost(url)
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON)
httpPost.setEntity(entity)
httpPost.setVersion(HttpVersion.HTTP_2)
return executeRequest(httpPost)
}
private static Map executeRequest(HttpPost request) {
try {
return client.execute(request) { ClassicHttpResponse response ->
HttpEntity entity = response.getEntity()
String body = entity ? EntityUtils.toString(entity) : null
int code = response.getCode()
return [response: body, code: code]
}
} catch (IOException e) {
println "HTTP call failed: ${e.getClass().simpleName} - ${e.getMessage()}"
throw new RuntimeException("HTTP request failed", e)
}
}
}
Key changes for HttpClient 5.x:
Updated imports : All imports now use org.apache.hc.client5.* and org.apache.hc.core5.* packages
HTTP/2 enforcement : Added .setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2) to force HTTP/2 connections
Built-in retry : Using DefaultHttpRequestRetryStrategy(3, TimeValue.ofSeconds(5)) for 3 retries with 5-second intervals
Modern timeout configuration : Using Timeout.of() for connection and response timeouts
Simplified execution : Using the lambda-style execute() method that automatically handles response closing
HTTP/2 version setting : Explicitly setting HttpVersion.HTTP_2 on each request
Removed custom retry logic : The client now handles retries automatically
The client is now configured to:
Use HTTP/2 by default
Retry failed requests up to 3 times with 5-second delays
Maintain connection pooling with up to 200 concurrent connections
Set appropriate timeouts (2 minutes for connection, 10 minutes for response)
The interface remains exactly the same - postForm() and postJson() methods work identically, but now with HTTP/2 support and built-in retry handling.