You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Jeff <pr...@gmail.com> on 2011/09/29 05:54:01 UTC

HttpClient 4.1.2 and redirect handling...

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
predatorvi@gmail.com
See my LinkedIn profile at:
http://www.linkedin.com/in/rjeffreyvincent
I ♥ DropBox <http://db.tt/9O6LfBX> !!

Re: HttpClient 4.1.2 and redirect handling...

Posted by Jeff <pr...@gmail.com>.
Thanks for the responses.  Works beautifully!  Looking forward to 4.2 as
well....
On Thu, Sep 29, 2011 at 9:54 AM, Vasile Alin <al...@gmail.com>wrote:

> You can also have a look at
>
> https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/LaxRedirectStrategy.java
> available starting with 4.2.
>
> On 29 September 2011 08:20, Ken Krugler <kk...@transpac.com>
> wrote:
> > 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
> >> predatorvi@gmail.com
> >> 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
> >
> >
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>


-- 
Jeff Vincent
predatorvi@gmail.com
See my LinkedIn profile at:
http://www.linkedin.com/in/rjeffreyvincent
I ♥ DropBox <http://db.tt/9O6LfBX> !!

Re: HttpClient 4.1.2 and redirect handling...

Posted by Vasile Alin <al...@gmail.com>.
You can also have a look at
https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/LaxRedirectStrategy.java
available starting with 4.2.

On 29 September 2011 08:20, Ken Krugler <kk...@transpac.com> wrote:
> 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
>> predatorvi@gmail.com
>> 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
>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: HttpClient 4.1.2 and redirect handling...

Posted by Ken Krugler <kk...@transpac.com>.
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
> predatorvi@gmail.com
> 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