How to enter the site?

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

Q1. Where do I specify the URL of the login page (in the new HTTPGET (".....") I guess)? Am I right or wrong?

Q2. What argument to pass to the HTTPPost (") constructor. If we need to pass the" action "attribute value of the" form "element in the html source of the login web page, please submit it.

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

onsubmit="return(gaia_onLoginSubmit());"

      

Are there any changes needed in the following code due to the above mentioned attribute?

Q4. How can I get the html source of web pages 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();        

      

+2


source to share


1 answer


1:

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

Session cookies can be obtained from response headers (or their cookies) and then added as attributes for future HTTP GET for the site or in request headers.

Q2:

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



Q3:

Probably no.

Q4:

use Method.getResponseBodyAsString OR Method.getResponseBody OR Method.getResponseBodyAsStream after HTTP GET to get a response that will contain the HTML source for this page.

+5


source







All Articles