简体   繁体   English

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

[英]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.我正在尝试命中 post 端点,但它给出了错误 302,当我在同一台服务器上尝试另一个 get Url 时,它给了我 200。然后我使用 LaxRedirectStrategy() 重定向了 post 请求,post 请求正在重定向到 get 请求(相同端点唯一的方法名称是 GET 和 POST)它没有从 post 方法获得响应。 Can anyone tell me how to redirect post request to post request using apahce httpClient 4.5谁能告诉我如何使用 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.我遇到了同样的问题,我通过使用LaxRedirectStrategy和重写的getRedirect方法来解决它。

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

See: DefaultRedirectStrategy which LaxRedirectStrategy inherits from.请参阅:LaxRedirectStrategy 继承自的DefaultRedirectStrategy

In my case the redirect response code was a 302.就我而言,重定向响应代码是 302。

So if you want something different, you can just override the getRedirect method and provide your own implementation.因此,如果您想要不同的东西,您可以只覆盖 getRedirect 方法并提供您自己的实现。

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

        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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