简体   繁体   中英

Apache HttpClient 4.5 redirect POST request to GET request

Am traying to hit a post endpoint but It is giving error 302, When I tried another get Url on the same server it gives me 200. Then I redirected the post request using LaxRedirectStrategy() The post request is redirecting to the get request(same endpoint only method name is GET and POST) it is not getting response from the post method. Can anyone tell me how to redirect post request to post request using apahce httpClient 4.5

HttpClient client= HttpClientBuilder.create()
 .setRedirectStrategy(new LaxRedirectStrategy()).build();
HttpPost post = new HttpPost("url");
post.addHeader("content-type", " application/json");
HttpResponse response = client.execute(post);

I had the same issue I solved it by using using LaxRedirectStrategy with overridden getRedirect method.

Apparently the default behaviour for POST requests is to make the redirected call as a GET request when the initial redirect response is different than 307 or 308.

See: DefaultRedirectStrategy which LaxRedirectStrategy inherits from.

In my case the redirect response code was a 302.

So if you want something different, you can just override the getRedirect method and provide your own implementation.

Something like:

new LaxRedirectStrategy() {
        @Override
        public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
            final URI uri = getLocationURI(request, response, context);
            final String method = request.getRequestLine().getMethod();
            if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
                return new HttpHead(uri);
            } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
                return new HttpGet(uri);
            } else {
                final int status = response.getStatusLine().getStatusCode();
                if (status == HttpStatus.SC_TEMPORARY_REDIRECT || status == HttpStatus.SC_MOVED_TEMPORARILY) { //HttpStatus.SC_MOVED_TEMPORARILY == 302
                    return RequestBuilder.copy(request).setUri(uri).build();
                } else {
                    return new HttpGet(uri);
                }
            }
        }
    }
    HttpClient httpClient =
        HttpClients.custom().setRedirectStrategy(new LaxRedirectStrategy() {

            /*
             * (non-Javadoc)
             * 
             * @see org.apache.http.impl.client.DefaultRedirectStrategy#
             * getRedirect(org.apache.http.HttpRequest,
             * org.apache.http.HttpResponse,
             * org.apache.http.protocol.HttpContext)
             */
            @Override
            public HttpUriRequest getRedirect(
                HttpRequest request, HttpResponse response,
                HttpContext context) throws ProtocolException
        {

                final URI uri = getLocationURI(request, response, context);
                final String method = request.getRequestLine().getMethod();
                if (method.equalsIgnoreCase(HttpPost.METHOD_NAME)) {

                    HttpPost post = new HttpPost(uri);
                    post.setEntity(entity);
                    return post;
                } else if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
                    return new HttpHead(uri);
                } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
                    return new HttpGet(uri);
                } else {
                    final int status =
                        response.getStatusLine().getStatusCode();
                    return status == HttpStatus.SC_TEMPORARY_REDIRECT
                        ? RequestBuilder.copy(request).setUri(uri).build()
                        : new HttpGet(uri);
                }
            }

        })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM