gpt4 book ai didi

java - 如何使用 Apache httpComponent5 设置 Spring HttpComponentsClientHttpRequestFactory?

转载 作者:行者123 更新时间:2023-12-01 17:26:00 35 4
gpt4 key购买 nike

我正在尝试在 Spring 中设置 httpClient5 ...我有以下代码:

PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(SSLConnectionSocketFactoryBuilder.create()
.setSslContext(SSLContexts.createSystemDefault())
.setTlsVersions(TLS.V_1_3, TLS.V_1_2)
.build())
.setDefaultSocketConfig(SocketConfig.custom()
.setSoTimeout(Timeout.ofSeconds(5))
.build())
.setPoolConcurrencyPolicy(PoolConcurrencyPolicy.STRICT)
.setConnPoolPolicy(PoolReusePolicy.LIFO)
.setConnectionTimeToLive(TimeValue.ofMinutes(1L))
.build();

CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connectionManager)
.setDefaultRequestConfig(RequestConfig.custom()
.setConnectTimeout(Timeout.ofSeconds(5))
.setResponseTimeout(Timeout.ofSeconds(5))
.setCookieSpec(StandardCookieSpec.STRICT)
.build())
.build();

CookieStore cookieStore = new BasicCookieStore();

CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

HttpClientContext clientContext = HttpClientContext.create();
clientContext.setCookieStore(cookieStore);
clientContext.setCredentialsProvider(credentialsProvider);
clientContext.setRequestConfig(RequestConfig.custom()
.setConnectTimeout(Timeout.ofSeconds(5000))
.setResponseTimeout(Timeout.ofSeconds(5000))
.build());



// connect Spring httpComponent (client-side) with Apache httpClient
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setReadTimeout(60000);
httpRequestFactory.setConnectTimeout(60000);
httpRequestFactory.setHttpClient(client);

类 ClosableHttpClient 实现了与 setHttpClient(client) 不兼容的接口(interface) org.apache.hc.client5.http.classic.HttpClient ;这是运行时错误:

java.lang.ClassCastException: class org.apache.hc.client5.http.impl.classic.InternalHttpClient cannot be cast to class org.apache.http.client.HttpClient (org.apache.hc.client5.http.impl.classic.InternalHttpClient and org.apache.http.client.HttpClient are in unnamed module of loader 'app')

我正在使用 Spring 5.0 和 HttpComponents-client-5。如何将 HttpClient 4.X 转换为 HttpClient 5?

最佳答案

Apache HttpClient 4.5 和 HttpClient 5.0 classic 非常相似,但 API 完全不兼容。

在 Spring 提供基于 Apache HttpClient 5.x 的 RequestFactory 实现之前,无法将 HttpClient 5.x 与 Spring 集成。

关于java - 如何使用 Apache httpComponent5 设置 Spring HttpComponentsClientHttpRequestFactory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61210545/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com