Skip to content

HttpComponentsClientHttpRequestFactory setReadTimeout not working with httpclient 5.4 #33806

Closed
@tvahrst

Description

@tvahrst

httpcomponents httpclient5 changed the internal implementation of HttpClientContext with version 5.4

apache/httpcomponents-client@762b18f
using instance variables for standard attributes instead of map-like attributes.

With this change, the current implementationof HttpComponentClientHttpRequestFactory#createRequest does not work (anymore), because it uses the old context.getAttribute() and context.setAttribute() Methods which do not have any effect anymore (at least on HttpClientContext instances)

	public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
        ...
		// Request configuration not set in the context
		if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {

        ....
			if (config != null) {
				context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
			}
		}
		return new HttpComponentsClientHttpRequest(client, httpRequest, context);
	}

As an side effect, the new support for readtimeout isnt working for RestTemplates und RestClients

A solution might be to use the getter/setter Methods for RequestConfig:

		HttpClientContext httpClientContext = HttpClientContext.cast(context);

		// Request configuration not set in the context
		if (httpClientContext.getRequestConfig() == null) {
			// Use request configuration given by the user, when available
			RequestConfig config = null;
			if (httpRequest instanceof Configurable configurable) {
				config = configurable.getConfig();
			}
			if (config == null) {
				config = createRequestConfig(client);
			}
			if (config != null) {
				httpClientContext.setRequestConfig(config);
			}
		}
		return new HttpComponentsClientHttpRequest(client, httpRequest, context);

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions