Note that there are some explanatory texts on larger screens.

plurals
  1. POPreventing HttpClient 4 from following redirect
    primarykey
    data
    text
    <p>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 (<a href="http://myapp.appspot.com/_ah/login?auth=.." rel="noreferrer">http://myapp.appspot.com/_ah/login?auth=..</a>.) 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.</p> <p>Fwiw, the actual method I use to send the request is below.</p> <pre><code>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 } } } </code></pre> <p>How would I stop the client from following the redirect?</p> <p>Thanks.</p> <p><strong>Update</strong>:</p> <p>As per the solution below, I did the following after creating a <code>DefaultHttpClient client</code> (and before passing it to the <code>execute</code> method):</p> <pre><code>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; } }); } </code></pre> <p>More verbose than it seems it needs to be, but not as difficult as I thought.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload