Apache HttpClient 4.5 重定向 POST 请求到 GET 请求

Apache HttpClient 4.5 redirect POST request to GET request

我正在尝试访问 post 端点,但出现错误 302,当我在同一台服务器上尝试另一个 get Url 时,它给了我 200。然后我重定向了 post 使用 LaxRedirectStrategy() 的请求 post 请求正在重定向到 get 请求(相同的端点只有方法名称是 GET 和 POST)它没有从 post 方法获得响应。谁能告诉我如何使用 apahce httpClient 4.5

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

我遇到了同样的问题,我通过使用 LaxRedirectStrategy 和覆盖的 getRedirect 方法解决了它。

显然,当初始重定向响应不同于 307 或 308 时,POST 请求的默认行为是将重定向调用作为 GET 请求。

见: DefaultRedirectStrategy LaxRedirectStrategy 继承自。

在我的例子中,重定向响应代码是 302。

因此,如果您想要不同的东西,您可以覆盖 getRedirect 方法并提供您自己的实现。

类似于:

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);
                }
            }

        })