Java Apache HttpClient 4.5重定向POST请求以获取请求

Java Apache HttpClient 4.5重定向POST请求以获取请求,java,spring-boot,apache-httpclient-4.x,Java,Spring Boot,Apache Httpclient 4.x,我试图点击一个post端点,但它给出了错误302,当我在同一台服务器上尝试另一个get Url时,它给出了200。然后,我使用laxrirectstrategy()重定向了post请求。post请求正在重定向到get请求(只有同一端点的方法名为get和post),但没有从post方法获得响应。有人能告诉我如何使用apahce httpClient 4.5将post请求重定向到post请求吗 HttpClient client= HttpClientBuilder.create() .setRe

我试图点击一个post端点,但它给出了错误302,当我在同一台服务器上尝试另一个get Url时,它给出了200。然后,我使用laxrirectstrategy()重定向了post请求。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);

我有同样的问题,我通过使用laxrirectstrategy和覆盖的getRedirect方法解决了这个问题

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

见: 该策略继承自

在我的例子中,重定向响应代码是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);
                }
            }
        }
    }

您从POST请求中得到的错误是什么?302?是的,302重定向只是为了安全起见,您可以尝试以“旧”方式处理重定向[检查状态是否为3XX并在位置标头中查找重定向主机],以确保问题不在您的服务器中。
    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);
                }
            }

        })