3

I need to follow redirects given to me by HTTPost. When I make an HTTPost, and try to read the response, I get the redirect's page html. How can I fix this? Code:

public void parseDoc() {
    final HttpParams params = new BasicHttpParams();
    HttpClientParams.setRedirecting(params, true);
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(
            "https://secure.groupfusion.net/processlogin.php");
    String HTML = "";
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("referral_page",
                "/modules/gradebook/ui/gradebook.phtml?type=student_view"));
        nameValuePairs.add(new BasicNameValuePair("currDomain",
                "beardenhs.knoxschools.org"));
        nameValuePairs.add(new BasicNameValuePair("username", username
                .getText().toString()));
        nameValuePairs.add(new BasicNameValuePair("password", password
                .getText().toString()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        String g = httppost.getURI().toString();

        HttpResponse response = httpclient.execute(httppost);

        HTML = EntityUtils.toString(response.getEntity());
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String ResponseBody = httpclient.execute(httppost, responseHandler);
        sting.setText(HTML);

    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

}
Bob
  • 22,810
  • 38
  • 143
  • 225
Eli
  • 262
  • 5
  • 15

2 Answers2

8

When a server sends a redirect, it is actually sending a 3xx response code (usually 301 or 302) that indicates the redirect, and a Location header that tells you the new location.

So, in your case, you can get the Location header from the HttpResponse object and use that to send another request to retrieve the actual content after you've logged in. For example:

String newUrl = response.getFirstHeader("Location").getValue();

So long as you reuse the same HttpClient object for both requests, it should use any cookies set by the login request in your subsequent request(s).

Spencer Uresk
  • 3,730
  • 26
  • 23
  • Thanks for your help, but I looked at the source code of the redirect page, and it's using javascript on.Load method to redirect me. Will this method still work? Thanks. – Eli Dec 11 '11 at 13:38
  • Unfortunately, no. I'm unaware of any HTTP client library that can follow JavaScript redirects. Unless you know beforehand the URL you want to request after logging in, I think your only option would be to parse out the URL from the response content. – Spencer Uresk Dec 11 '11 at 20:17
  • @Eli kindly if you share the code, i am stuck in same problem ? – Saad Bilal Feb 24 '14 at 10:27
3

Try using the HttpGet method

GetMethods will follow redirect requests from the http server by default. This behavour can be disabled by calling setFollowRedirects(false).

For more info refer this

Hope it helps,

Cheers

Parth Doshi
  • 4,200
  • 15
  • 79
  • 129
  • Thanks for the reply, but I need to log into this site using a post method, then get the source code of the home page. HTTPGet will fail the login, wouldn't it? Because it would put the login credentials in the url. – Eli Dec 11 '11 at 06:02
  • Yes your right, but then I don't know of any other way using which you can follow the redirect. – Parth Doshi Dec 11 '11 at 06:10
  • Would you suggest any other approach to my problem? I just need to log into this website and get the homepage content. Anything that can accomplish this will be appreciated. Thanks. – Eli Dec 11 '11 at 06:16
  • Sorry, I have no idea about the login part but if you want to get just the content refer this question:http://stackoverflow.com/questions/5166143/how-to-get-content-of-a-url-and-to-read-it-in-a-android-java-application-using-e/5166257#5166257 – Parth Doshi Dec 11 '11 at 06:22