2

Possible Duplicate:
Httpclient 4, error 302. How to redirect?

I want to retrieve some information from my comcast account. Using examples on this site, I think I got pretty close. I am using firebug to see what to post, and I see that when I login I am being redirected. I don't understand how to follow the redirects. I have played with countless examples but just can't figure it out. I am new to programming and just not having any luck doing this. Here is my code. I make an initial login, then go to try to go to another URL which is where the redirects begin. Along the way, I see that I am acquiring lots of cookies, but not the important one s_lst.

HttpPost httpPost = new HttpPost("https://login.comcast.net/login");

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("continue", "https://login.comcast.net/account"));
        nvps.add(new BasicNameValuePair("deviceAuthn", "false"));
        nvps.add(new BasicNameValuePair("forceAuthn", "true"));
        nvps.add(new BasicNameValuePair("ipAddrAuthn", "false"));
        nvps.add(new BasicNameValuePair("lang", "en"));
        nvps.add(new BasicNameValuePair("passwd", "mypassword"));
        nvps.add(new BasicNameValuePair("r", "comcast.net"));
        nvps.add(new BasicNameValuePair("rm", "2"));
        nvps.add(new BasicNameValuePair("s", "ccentral-cima"));
        nvps.add(new BasicNameValuePair("user", "me"));

        httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        System.out.println("executing request " + httpPost.getURI());
        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpPost, responseHandler);
        String cima = StringUtils.substringBetween(responseBody, "cima.ticket\" value=\"", "\">");
        System.out.println(cima);

        HttpPost httpPost2 = new HttpPost("https://customer.comcast.com/Secure/Home.aspx");

        List <NameValuePair> nvps2 = new ArrayList <NameValuePair>();
        nvps2.add(new BasicNameValuePair("cima.ticket", cima));

        httpPost2.setEntity(new UrlEncodedFormEntity(nvps2, HTTP.UTF_8));

        System.out.println("executing request " + httpPost2.getURI());
        // Create a response handler
        ResponseHandler<String> responseHandler2 = new BasicResponseHandler();
        String responseBody2 = httpclient.execute(httpPost2, responseHandler2);
        System.out.println(responseBody2);
Community
  • 1
  • 1
Brian
  • 489
  • 3
  • 9
  • 13
  • Does this solution work for you? http://stackoverflow.com/questions/3658721/httpclient-4-error-302-how-to-redirect – Martin Dow Sep 25 '11 at 17:28
  • I'm not sure where to put the code. I edited the post above with my best guess. It doesn't compile I think because I don't have a context anywhere in my code. Not sure what that does. – Brian Sep 25 '11 at 19:50
  • Also, this seems code seems like a test. It will return true or false based on whether there is a 301 or 302. I already know there is a 302. I guess I was trying to manually go to the sites in the order that the redirects take a user, and try to acquire all of the required cookies so I can get the information I need on the final page. Is this the wrong approach? – Brian Sep 25 '11 at 19:59
  • Which version of HTTPClient are you using? – Martin Dow Sep 25 '11 at 20:24
  • Just add the `client.setRedirectHandler(new DefaultRedirectHandler() { ... }` method call after your `DefaultHttpClient httpclient = new DefaultHttpClient();` line. Looks like that should enable redirects. I see you've added it, but it needs to be called before you make the request, so do it straight after you instantiate the `DefaultHttpClient`. – Martin Dow Sep 25 '11 at 20:33
  • I added this line: httpclient.setRedirectStrategy(new DefaultRedirectStrategy()); right after DefaultHttpClient but got the same output. I'm not sure if that is what you were trying to get me to do. I am also not sure if I need any of the other lines of code (the true/false test). I am using 4.1.2 by the way. Thanks. – Brian Sep 25 '11 at 22:23

1 Answers1

4

Here's a sample adapted from the 'Response Handling' example here.

Your example is quite complicated - best to simplify your code while you figure out how to follow redirects (you can comment out the section I've highlighted to show the example failing to follow the redirect).

import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.protocol.*;

public class ClientWithResponseHandler {

    public final static void main(String[] args) throws Exception {

        DefaultHttpClient httpclient = new DefaultHttpClient();
        // Comment out from here (Using /* and */)...
        httpclient.setRedirectStrategy(new DefaultRedirectStrategy() {                
          public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)  {
            boolean isRedirect=false;
            try {
              isRedirect = super.isRedirected(request, response, context);
            } catch (ProtocolException e) {
              e.printStackTrace();
            }
            if (!isRedirect) {
              int responseCode = response.getStatusLine().getStatusCode();
              if (responseCode == 301 || responseCode == 302) {
                return true;
              }
            }
            return false;
          }
        });
        // ...to here and the request will fail with "HttpResponseException: Moved Permanently"
        try {
            HttpPost httpPost = new HttpPost("http://news.bbc.co.uk/");
            System.out.println("executing request " + httpPost.getURI());
            // Create a response handler
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpPost, responseHandler);
            System.out.println(responseBody);
            // Add your code here...
        } finally {
            // When HttpClient instance is no longer needed, shut down the connection 
            // manager to ensure immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }

}
Martin Dow
  • 5,273
  • 4
  • 30
  • 43
  • I'm editing my response to reflect my new code based on your suggestions. I will show what is between try and finally in your proposed answer. The first responseBody returns as expected. I am not sure I am performing the second request correct to allow redirects. The first URL which must be submitted to login receives a 200. It is the second request that gets a 302. According to firebug, a cima.ticket post must be submitted, so I captured that from responseBody and put it into the post array. I am getting an error HttpResponse exception found. – Brian Sep 28 '11 at 00:35
  • I think what one problem may be is that you can only be redirected to the form, but my guess is that you are only submitting form data for the site that redirects you. Is there a way to get it to submit form data for ever redirect? – Brian Sep 29 '11 at 00:13