diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/ConnectExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/ConnectExec.java index 1e83a2443..af8daf78f 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/ConnectExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/ConnectExec.java @@ -202,10 +202,6 @@ public final class ConnectExec implements ExecChainHandler { this.proxyHttpProcessor.process(connect, null, context); while (response == null) { - if (!execRuntime.isConnected()) { - execRuntime.connect(context); - } - connect.removeHeaders(HttpHeaders.PROXY_AUTHORIZATION); this.authenticator.addAuthResponse(proxy, ChallengeType.PROXY, connect, proxyAuthExchange, context); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/ExecRuntimeImpl.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/ExecRuntimeImpl.java index 67d5ec065..e80956f1e 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/ExecRuntimeImpl.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/sync/ExecRuntimeImpl.java @@ -141,22 +141,26 @@ class ExecRuntimeImpl implements ExecRuntime, Cancellable { return endpoint != null && endpoint.isConnected(); } + private void connectEndpoint(final ConnectionEndpoint endpoint, final HttpClientContext context) throws IOException { + if (cancellableAware != null) { + if (cancellableAware.isCancelled()) { + throw new RequestFailedException("Request aborted"); + } + } + final RequestConfig requestConfig = context.getRequestConfig(); + final TimeValue connectTimeout = requestConfig.getConnectTimeout(); + manager.connect(endpoint, connectTimeout, context); + final TimeValue socketTimeout = requestConfig.getSocketTimeout(); + if (socketTimeout.getDuration() >= 0) { + endpoint.setSocketTimeout(socketTimeout.toMillisIntBound()); + } + } + @Override public void connect(final HttpClientContext context) throws IOException { final ConnectionEndpoint endpoint = ensureValid(); if (!endpoint.isConnected()) { - if (cancellableAware != null) { - if (cancellableAware.isCancelled()) { - throw new RequestFailedException("Request aborted"); - } - } - final RequestConfig requestConfig = context.getRequestConfig(); - final TimeValue connectTimeout = requestConfig.getConnectTimeout(); - manager.connect(endpoint, connectTimeout, context); - final TimeValue socketTimeout = requestConfig.getSocketTimeout(); - if (socketTimeout.getDuration() >= 0) { - endpoint.setSocketTimeout(socketTimeout.toMillisIntBound()); - } + connectEndpoint(endpoint, context); } } @@ -178,6 +182,9 @@ class ExecRuntimeImpl implements ExecRuntime, Cancellable { @Override public ClassicHttpResponse execute(final ClassicHttpRequest request, final HttpClientContext context) throws IOException, HttpException { final ConnectionEndpoint endpoint = ensureValid(); + if (!endpoint.isConnected()) { + connectEndpoint(endpoint, context); + } return endpoint.execute(request, requestExecutor, context); }