Description
Jean-Pierre Bergamin opened SPR-12540 and commented
HttpComponentsClientHttpRequestFactory.createRequest
always sets the HttpClientContext.REQUEST_CONFIG
attribute of the HttpContext
- also with a default config if none can be found.
The HttpClientBuilder
on the other hand allows to set a default RequestConfig
with:
RequestConfig requestConfig = RequestConfig.custom()
.setCookieSpec(NO_SPEC_PROVIDER)
.build();
HttpClient httpClient = HttpClientBuilder.create().
....
setDefaultRequestConfig(requestConfig).
...
build();
The returned InternalHttpClient
by the builder later sets this configured default requestConfig
if no other request config has been set:
private void setupContext(final HttpClientContext context) {
....
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
context.setAttribute(HttpClientContext.REQUEST_CONFIG, this.defaultConfig);
}
}
But since a default RequestConfig
has been set in HttpComponentsClientHttpRequestFactory.createRequest
, it is not applied here and the configured RequestConfig in the HttpClientBuilder is not used.
As a workaround you have to create your own HttpComponentsClientHttpRequestFactory
implementation and overwrite createHttpContext
to return a configured HttpContext
like:
public class StatefullHttpComponentsClientHttpRequestFactory extends HttpComponentsClientHttpRequestFactory
{
private final HttpContext httpContext;
public StatefullHttpComponentsClientHttpRequestFactory(HttpClient httpClient, HttpContext httpContext)
{
super(httpClient);
this.httpContext = httpContext;
}
@Override
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri)
{
return this.httpContext;
}
}
HttpContext context = new BasicHttpContext();
context.setAttribute(HttpClientContext.REQUEST_CONFIG, requestConfig);
ClientHttpRequestFactory requestFactory = new ContextAwareHttpComponentsClientHttpRequestFactory(httpClient, context));
RestOperations restClient = new RestTemplate(requestFactory);
Conclusion: Do not set a default RequestConfig
in HttpComponentsClientHttpRequestFactory
if the http client is of instance InternalHttpClient
and therefore configured by the HttpClientBuilder.
Issue Links:
- When using HttpComponentsClientHttpRequestFactory, setting a proxy through RequestConfig should not be ignored [SPR-12335] #16940 When using HttpComponentsClientHttpRequestFactory, setting a proxy through RequestConfig should not be ignored ("duplicates")
- Upgrade HttpComponentsHttpInvokerRequestExecutor to require Apache HttpComponents 4.3 [SPR-11113] #15739 Upgrade HttpComponentsHttpInvokerRequestExecutor to require Apache HttpComponents 4.3
- Merge default request config with local customizations [SPR-12583] #17184 Merge default request config with local customizations
- HttpComponentsClientHttpRequestFactory does not set connection request timeout on request config [SPR-12166] #16780 HttpComponentsClientHttpRequestFactory does not set connection request timeout on request config
- HttpComponentsAsyncClientHttpRequestFactory overrides default request config of httpclient [SPR-13125] #17716 HttpComponentsAsyncClientHttpRequestFactory overrides default request config of httpclient
Referenced from: commits 71783c5, 5236eb6
1 votes, 7 watchers