Preventing HttpClient 4 from following redirect

后端 未结 5 1390
礼貌的吻别
礼貌的吻别 2020-12-01 14:19

I\'m connecting to my AppEngine application using the Apache HttpComponents library. In order to authenticate my users, I need to pass an authentication token along to the

相关标签:
5条回答
  • 2020-12-01 14:45

    Try using a RedirectHandler. That may require extending DefaultHttpClient to return your custom implementation from createRedirectHandler().

    0 讨论(0)
  • 2020-12-01 14:50

    The RedirectHandler seem to be deprecated, I managed to disable automatically following the redirect responses by changing the default RedirectionStrategy for the DefaultHttpClient liks this:

    httpClient.setRedirectStrategy(new RedirectStrategy() {
            @Override
            public boolean isRedirected(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws ProtocolException {
                return false;
            }
    
            @Override
            public HttpUriRequest getRedirect(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext) throws ProtocolException {
                return null;
            }
        });
    

    On the downside, this tie us to a specific implementation of the HttpClient but it does the job.

    0 讨论(0)
  • 2020-12-01 15:00

    a quick google presented: http://hc.apache.org/httpclient-3.x/redirects.html

    0 讨论(0)
  • 2020-12-01 15:01

    With the Version 4.3.x of HttpClient its directly in the clientBuilder.

    so when u build your Client use:

    CloseableHttpClient client = clientBuilder.disableRedirectHandling().build();
    

    I know its an old question but I had this problem too and want to share my solution.

    0 讨论(0)
  • 2020-12-01 15:12

    You can do it with the http params:

    final HttpParams params = new BasicHttpParams();
    HttpClientParams.setRedirecting(params, false);
    

    The method has no javadoc, but if you look at the source you can see it sets:

    HANDLE_REDIRECTS

    which controls:

    Defines whether redirects should be handled automatically

    0 讨论(0)
提交回复
热议问题