【问题标题】:Apache HttpClient 4.5 redirect POST request to GET requestApache HttpClient 4.5 将 POST 请求重定向到 GET 请求
【发布时间】:2019-10-12 05:59:44
【问题描述】:

我正在尝试命中一个 post 端点,但它给出了错误 302,当我在同一台服务器上尝试另一个 get Url 时,它给了我 200。然后我使用 LaxRedirectStrategy() 重定向了 post 请求 post 请求正在重定向到 get请求(同一端点唯一的方法名称是 GET 和 POST)它没有从 post 方法获得响应。谁能告诉我如何使用 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);

【问题讨论】:

  • 您从 POST 请求中得到的错误是什么? 302?
  • 是的,302 重定向
  • 为了安全起见,您可以尝试以“旧”方式处理重定向 [检查状态是否为 3XX 并在位置标头中查找重定向主机] 以确保问题不在您的服务器中。 stackoverflow.com/questions/8014997/…

标签: java spring-boot apache-httpclient-4.x


【解决方案1】:

我遇到了同样的问题,我通过使用 LaxRedirectStrategy 和重写的 getRedirect 方法解决了这个问题。

显然,POST 请求的默认行为是在初始重定向响应不同于 307 或 308 时将重定向调用作为 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);
                }
            }
        }
    }

【讨论】:

    【解决方案2】:
        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);
                    }
                }
    
            })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 2012-12-24
      • 2022-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多