views:

403

answers:

1

I want to login to the ORKUT through a java program. I am using the following program to do it. I have copied it from some website. Now I want to use it for ORKUT. But I have some questions regarding some lines.

Q1. Where to give the URL of the login page (I think in the new HTTPGET("....."))? Am I right or not?

Q2. What argument to pass to the constructor of HTTPPost(""). If we have to pass the value of the "action" attribute of the "form" element in the html source of the login webpage, then please confirm it.

Q3. The "form" element of the ORKUT login page has the attribute

onsubmit="return(gaia_onLoginSubmit());"

Do I need any changes in the following code due to the presence of above attribute ?

Q4. How can I get html source of webpages after login?

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;


public class ClientFormLogin {

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

    DefaultHttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget = new HttpGet("https://www.google.com/accounts/ServiceLogin?service=orkut&hl=en-US&rm=false&continue=http%3A%2F%2Fwww.orkut.com%2FRedirLogin%3Fmsg%3D0%26page%3Dhttp%253A%252F%252Fwww.orkut.co.in%252FHome.aspx&cd=IN&passive=true&skipvpage=true&sendvemail=false");

    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("https://www.google.com/accounts/ServiceLoginAuth?service=orkut");

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

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

    response = httpclient.execute(httpost);
    entity = response.getEntity();

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

    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());
        }
    }

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();
+3  A: 

Q1:

The standard way to do this is a HTTP POST to the login URL with login information as parameters in the method body. This usually is username and password (or potentially, a hash of the password).

The session cookies can be retrieved from the response headers (or their cookies), and then either added as attributes to a future HTTP GET for the site, or as request headers.

Q2:

I think it depends on the site. Not sure -- try tinkering with Firefox and the Live HTTP Headers extension.

Q3:

Probably not.

Q4:

use the Method.getResponseBodyAsString OR Method.getResponseBody OR Method.getResponseBodyAsStream after an HTTP GET to retrieve the response, which will contain the HTML source for the page.


Please remember to upvote and ACCEPT answers to your questions! This adds to your reputation, and rewards people who have taken the time to help you. Your current accept rate of 0% discourages good answers, because people know they won't gain rep for answering!

BobMcGee
The above code was working fine when I used it to login to other site but I am not getting expected results when I logged in to the ORKUT. Is the problem with "https" as ORKUT use "HTTPS"
Yatendra Goel
What do you mean by "not getting expected results"? What sort of error is it getting?HTTPS *could* be the problem, although I think HTTPClient should automatically handle that. It is pretty smart, although I didn't have to worry about https in the past.
BobMcGee