diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java index c8ad47d92..53d5b7e34 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/AsyncRedirectExec.java @@ -138,20 +138,20 @@ public final class AsyncRedirectExec implements AsyncExecChainHandler { final int statusCode = response.getCode(); + state.currentRequest = null; switch (statusCode) { case HttpStatus.SC_MOVED_PERMANENTLY: case HttpStatus.SC_MOVED_TEMPORARILY: case HttpStatus.SC_SEE_OTHER: if (!Methods.isSafe(request.getMethod())) { - final HttpRequest httpGet = new BasicHttpRequest(Methods.GET, redirectUri); - httpGet.setHeaders(scope.originalRequest.getHeaders()); - state.currentRequest = httpGet; + state.currentRequest = new BasicHttpRequest(Methods.GET, redirectUri); state.currentEntityProducer = null; - break; } - default: - state.currentRequest = new BasicHttpRequest(request.getMethod(), redirectUri); } + if (state.currentRequest == null) { + state.currentRequest = new BasicHttpRequest(request.getMethod(), redirectUri); + } + state.currentRequest.setHeaders(scope.originalRequest.getHeaders()); final HttpHost newTarget = URIUtils.extractHost(redirectUri); if (newTarget == null) { throw new ProtocolException("Redirect URI does not specify a valid host name: " + redirectUri); diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java index 025d9f24f..d2503e638 100644 --- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java +++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/RedirectExec.java @@ -147,17 +147,14 @@ public final class RedirectExec implements ExecChainHandler { case HttpStatus.SC_MOVED_TEMPORARILY: case HttpStatus.SC_SEE_OTHER: if (!Methods.isSafe(request.getMethod())) { - final HttpGet httpGet = new HttpGet(redirectUri); - httpGet.setHeaders(originalRequest.getHeaders()); - redirect = httpGet; - } else { - redirect = null; + redirect = new HttpGet(redirectUri); } } if (redirect == null) { redirect = new BasicClassicHttpRequest(originalRequest.getMethod(), redirectUri); redirect.setEntity(originalRequest.getEntity()); } + redirect.setHeaders(originalRequest.getHeaders()); final HttpHost newTarget = URIUtils.extractHost(redirectUri); if (newTarget == null) {