hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gerd Schmidt <g.schmid...@gmx.de>
Subject 4.2.1 - 302 Redirect
Date Mon, 17 Sep 2012 13:39:24 GMT

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, 

             HttpResponse response = httpclient.execute(httpost);

             HttpEntity entity = response.getEntity();
             System.out.println("Login form get: " + 

         } finally {

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 
             String responseBody = httpclient.execute(httpget, 

But this is not the page, with the updated content i wanted. Furthermore 
I read, I have to set a "RedirectStrategy", so i tried

((AbstractHttpClient) httpclient).setRedirectStrategy(new 

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

View raw message