21

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 application's login address (http://myapp.appspot.com/_ah/login?auth=...) and grab a cookie from the header of the response. However, the login page responds with a redirect status code, and I don't know how to stop HttpClient from following the redirect, thus thwarting me from intercepting the cookie.

Fwiw, the actual method I use to send the request is below.

private void execute(HttpClient client, HttpRequestBase method) {
    // Set up an error handler
    BasicHttpResponse errorResponse = new BasicHttpResponse(
            new ProtocolVersion("HTTP_ERROR", 1, 1), 500, "ERROR");

    try {
        // Call HttpClient execute
        client.execute(method, this.responseHandler);
    } catch (Exception e) {
        errorResponse.setReasonPhrase(e.getMessage());
        try {
            this.responseHandler.handleResponse(errorResponse);
        } catch (Exception ex) {
            // log and/or handle
        }
    }
}

How would I stop the client from following the redirect?

Thanks.

Update:

As per the solution below, I did the following after creating a DefaultHttpClient client (and before passing it to the execute method):

if (!this.followRedirect) {
    client.setRedirectHandler(new RedirectHandler() {
        public URI getLocationURI(HttpResponse response,
                HttpContext context) throws ProtocolException {
            return null;
        }

        public boolean isRedirectRequested(HttpResponse response,
                HttpContext context) {
            return false;
        }
    });
}

More verbose than it seems it needs to be, but not as difficult as I thought.

mjumbewu
  • 1,104
  • 5
  • 14
  • 28
  • 1
    Note that once you have set a RedirectHandler that remains in effect for all subsequent Requests - not just the Request of interest here. So if you are are going on to issue further Requests which may require redirection to execute properly you will want to do a client.setRedirectHandler(new DefaultRedirectHandler()) after the execute. – Torid Apr 27 '11 at 19:25

5 Answers5

30

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

David Koski
  • 965
  • 7
  • 12
10

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.

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Kreuzbube
  • 103
  • 1
  • 3
  • 7
6

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    If you are using the amazing android-async-http library, I created a fork + pull request of it that has this behaviour: https://github.com/loopj/android-async-http/pull/184 – Daniel Ribeiro Feb 25 '13 at 08:17
1

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.

HackerMonkey
  • 443
  • 3
  • 13
0

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

Niko
  • 6,133
  • 2
  • 37
  • 49
  • Thanks for checking, but the link is for HttpClient 3. My issue is that I'm writing an Android application, and Android comes with HttpClient 4. Indeed, if I were using the Commons (3.x) HttpClient, I could just call setFollowRedirects(false) on my HttpMethod and be done with it. As far as I can tell (and someone please correct me if I'm wrong), it's a little more complicated (if possible?) with HttpComponents. – mjumbewu Aug 30 '09 at 16:39