0

I want to login to a site using HttpClient and after logging in I want to search for something and retrieve the contents of the search result.

/**
 * A example that demonstrates how HttpClient APIs can be used to perform
 * form-based logon.
 */
public class TestHttpClient {

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

        DefaultHttpClient httpclient = new DefaultHttpClient();

        HttpGet httpget = new HttpGet("http://projecteuler.net/");

        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {
            entity.consumeContent();
        }
        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }

        HttpPost httpost = new HttpPost("http://projecteuler.net/index.php?section=login");

        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("IDToken1", "username"));
        nvps.add(new BasicNameValuePair("IDToken2", "password"));

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

        response = httpclient.execute(httpost);

        System.out.println("Response "+response.toString());
        entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {

            InputStream is = entity.getContent();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String str ="";
            while ((str = br.readLine()) != null){
                System.out.println(""+str);
            }
        }

        System.out.println("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
        httpclient.getConnectionManager().shutdown();        
    }
}

when I print the output from HttpEntity it's printing the login page contents. How do I get the contents of the page after I login using HttpClient?

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
Santhosh S
  • 1,141
  • 6
  • 21
  • 43

1 Answers1

1

The post should mimick the form submit. No need to get the login page first. If I take a look at http://projecteuler.net, it seems the form is posted to index.php, so I'd try changing the post url:

HttpPost httpost = new HttpPost("http://projecteuler.net/index.php");

Use something like Fire bug to see what is exactly happening in the browser. Maybe you should follow a redirect after logging in (HttpClient supports this). There also seems to be a parameter called "login"with value "Login" that is being posted.

EJB
  • 2,383
  • 14
  • 15
  • thanks ... I am able to login and get the next page contents. If there is a search box in the next page ... how do I search and get the response using HttpClient – Santhosh S Jan 13 '10 at 18:43
  • use the same procedure: check the HTML which parameters should be posted to which URL... – EJB Jan 14 '10 at 08:20