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 Gerd Schmidt <g....@gmx.de> on 2012/09/17 15:39:24 UTC
4.2.1 - 302 Redirect
Hi,
I want to get the body from a page, which is redirected from a https adress.
I'm using Version 4.2.1 and Eclipse Java EE IDE for Web Developers
Version: Indigo Service Release 2
I read the tutorials and this is my code:
public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httpost = new HttpPost("https://xxx.xx");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("login", "abc"));
nvps.add(new BasicNameValuePair("password", "abc"));
httpost.setEntity(new UrlEncodedFormEntity(nvps,
Consts.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
System.out.println("Login form get: " +
response.getStatusLine());
EntityUtils.consume(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
}
It works, I get a "HTTP/1.1 302 Moved Temporarily", so I checked the
HEADER for the redirectLocation.
String redirectLocation = null;
Header locationHeader = response.getFirstHeader("location");
if (locationHeader != null) {
redirectLocation = locationHeader.getValue();
}
This works too, I got the new adress. My Problem is, I want to get the
body of the redirected location (while I'm logged in).
So i tried:
HttpGet httpget = new HttpGet(redirectLocation);
ResponseHandler<String> responseHandler = new
BasicResponseHandler();
String responseBody = httpclient.execute(httpget,
responseHandler);
System.out.println(responseBody);
But this is not the page, with the updated content i wanted. Furthermore
I read, I have to set a "RedirectStrategy", so i tried
putting
((AbstractHttpClient) httpclient).setRedirectStrategy(new
LaxRedirectStrategy());
after HttpClient httpclient = new DefaultHttpClient(); still no change.
(Anyway, why is httpclient.setRedirectStrategy(new
LaxRedirectStrategy()); not working?)
So, my Question is, "How do i handle a 302 redirect an get the
redirected body".
Thank you
Gerd Schmidt
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org
Re: 4.2.1 - 302 Redirect
Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2012-09-17 at 15:39 +0200, Gerd Schmidt wrote:
> Hi,
>
> I want to get the body from a page, which is redirected from a https adress.
>
> I'm using Version 4.2.1 and Eclipse Java EE IDE for Web Developers
> Version: Indigo Service Release 2
>
> I read the tutorials and this is my code:
>
> public final static void main(String[] args) throws Exception {
>
> HttpClient httpclient = new DefaultHttpClient();
>
> try {
>
> HttpPost httpost = new HttpPost("https://xxx.xx");
>
> List <NameValuePair> nvps = new ArrayList <NameValuePair>();
> nvps.add(new BasicNameValuePair("login", "abc"));
> nvps.add(new BasicNameValuePair("password", "abc"));
>
> httpost.setEntity(new UrlEncodedFormEntity(nvps,
> Consts.UTF_8));
>
> HttpResponse response = httpclient.execute(httpost);
>
> HttpEntity entity = response.getEntity();
> System.out.println("Login form get: " +
> response.getStatusLine());
> EntityUtils.consume(entity);
>
> } finally {
> httpclient.getConnectionManager().shutdown();
> }
> }
>
> It works, I get a "HTTP/1.1 302 Moved Temporarily", so I checked the
> HEADER for the redirectLocation.
>
> String redirectLocation = null;
> Header locationHeader = response.getFirstHeader("location");
> if (locationHeader != null) {
> redirectLocation = locationHeader.getValue();
> }
>
> This works too, I got the new adress. My Problem is, I want to get the
> body of the redirected location (while I'm logged in).
> So i tried:
>
> HttpGet httpget = new HttpGet(redirectLocation);
> ResponseHandler<String> responseHandler = new
> BasicResponseHandler();
> String responseBody = httpclient.execute(httpget,
> responseHandler);
> System.out.println(responseBody);
>
> But this is not the page, with the updated content i wanted. Furthermore
> I read, I have to set a "RedirectStrategy", so i tried
> putting
>
> ((AbstractHttpClient) httpclient).setRedirectStrategy(new
> LaxRedirectStrategy());
>
> after HttpClient httpclient = new DefaultHttpClient(); still no change.
> (Anyway, why is httpclient.setRedirectStrategy(new
> LaxRedirectStrategy()); not working?)
>
> So, my Question is, "How do i handle a 302 redirect an get the
> redirected body".
>
> Thank you
> Gerd Schmidt
>
Gerd,
You basically have two options: either implement a custom
RedirectStrategy (LaxRedirectStrategy is not going to help here as it is
intended for a completely different case) or disable automatic redirects
and handle all redirect status codes manually.
Oleg
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org