This is a topic that comes up repeatedly on the list.

From Ryan Smith's response to the same question about a year ago:

> Redirecting POSTs breaks RFC standards.  You need to extend and implement
> your own RedirectHandler/RedirectStrategy and tell HttpClient to use it
> instead of the default one.

I see that section 5.3 in the tutorial already says "HttpClient handles all 
types of redirects automatically, except those explicitly prohibited by the 
HTTP specification as requiring user intervention". Though I don't know what 
the next bit of text on the tutorial is trying to say: "See Other (status code 
303) redirects on POST and PUT requests..."

There's some code at Stack Overflow (first hit on 
http://lmgtfy.com/?q=httpclient+4+doesn%27t+redirect+post) that might be useful.

http://stackoverflow.com/questions/3658721/httpclient-4-error-302-how-to-redirect

-- Ken

On Sep 28, 2011, at 8:54pm, Jeff wrote:

> Being new to the v4.x HttpClient (and rusty with older versions...ack), this
> may be something obvious that I've just missed.
> 
> The tutorial (section 5.3) states that HttpClient handles most redirect
> automatically.  I have the following set:
> 
> 
> httpclient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS,
> true);
> 
> httpclient.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS,
> true);
> 
> I create the HttpPost for the form POST which seems to be working:
> 
>      post = new HttpPost(baseUrl + "/userLoginManual.aspx");
> 
>      List<NameValuePair> formparams = new ArrayList<NameValuePair>();
>      formparams.add(new BasicNameValuePair("postlogin_path",
> "/offers/consent.aspx"));
>      formparams.add(new BasicNameValuePair("login", username));
>      formparams.add(new BasicNameValuePair("password", password));
>      UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams,
> "UTF-8");
>      post.setEntity(entity);
> 
>      response = httpclient.execute(post, localContext);
> 
>      code = response.getStatusLine().getStatusCode();
> 
> But the response is "302 - Moved Temporarily".  This is part of the normal
> process, but I would have expected HttpClient to handle it and return me a
> 200.
> 
> Since I really only needed the cookies returned after successful auth, I am
> doing :
> 
>      try {
>        respStr = responseHandler.handleResponse(response);
>      } catch (HttpResponseException hrex) {
>        post.abort();
>      }
> in order to consume the response which is throwing the HttpResponseException
> (due to the 302) and calling post.abort().  This works for me, but I
> wondered why the redirect isn't happening automagically.
> 
> Any light/knowledge/insight is much appreciated.
> 
> -- 
> Jeff Vincent
> [email protected]
> See my LinkedIn profile at:
> http://www.linkedin.com/in/rjeffreyvincent
> I ♥ DropBox <http://db.tt/9O6LfBX> !!

--------------------------
Ken Krugler
+1 530-210-6378
http://bixolabs.com
custom big data solutions & training
Hadoop, Cascading, Mahout & Solr



Reply via email to