HttpClient API CODE--To acess mail

Discussion in 'Apache' started by bcprashanth, Sep 12, 2008.

  1. #1
    Hi
    I am new to this forum.
    I am using commons HttpClient API to access my gmail account.But whenever i execute the code i get only the html source of the corresponding(login page) page as output. I am not able to login into gmail.
    The code which i use is given below.Please suggest any changes or different code to pass credentials .



    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.UsernamePasswordCredentials;
    import org.apache.commons.httpclient.auth.AuthScope;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;

    /**
    * A simple example that uses HttpClient to perform a GET using Basic
    * Authentication. Can be run standalone without parameters.
    *
    * You need to have JSSE on your classpath for JDK prior to 1.4
    *

    */
    public class HttpClientBasicAuth {

    /**
    * Constructor for BasicAuthenticatonExample.
    */
    public HttpClientBasicAuth() {
    super();
    }

    public static void main(String[] args) throws Exception {
    HttpClient client = new HttpClient();


    // pass our credentials to HttpClient, they will only be used for
    // authenticating to servers with realm "realm" on the host
    // "www.verisign.com", to authenticate against
    // an arbitrary realm or host change the appropriate argument to null.
    client.getState().setCredentials(
    new AuthScope("http://www.gmail.com", 443, null),
    new UsernamePasswordCredentials("XXXXXXX", "YYYYYYY")
    );

    PostMethod post = new PostMethod("http://www.gmail.com");
    //post.addParameter("action", "../cgi-bin/webcm");
    //post.addParameter("target", "_self");
    //post.addParameter("getpage", "../html/menus/menu1.html");
    post.addParameter("USERNAME", "XXXXXXX ");
    post.addParameter("PASSWORD", "YYYYYYY");
    //post.addParameter("Login","Login");

    // create a GET method that reads a file over HTTPS, we're assuming
    // that this file requires basic authentication using the realm above.
    GetMethod get1 = new GetMethod("http://www.gmail.com");
    GetMethod get2 = new GetMethod("http://www.gmail.com");

    // Tell the GET method to automatically handle authentication. The
    // method will use any appropriate credentials to handle basic
    // authentication requests. Setting this value to false will cause
    // any request for authentication to return with a status of 401.
    // It will then be up to the client to handle the authentication.
    get1.setDoAuthentication( true );

    try {
    client.executeMethod(get1);
    if (get1.getStatusCode() == HttpStatus.SC_OK) {
    System.out.println(get1.getResponseBodyAsString());
    } else {
    System.out.println("Unexpected failure get1: " + get1.getStatusLine().toString());
    return;
    }
    get1.releaseConnection();

    // execute the POST
    client.executeMethod(post);
    if (post.getStatusCode() == HttpStatus.SC_OK) {
    System.out.println(post.getResponseBodyAsString());

    } else {
    System.out.println("Unexpected failure post: " + post.getStatusLine());
    return;
    }
    post.releaseConnection();

    // execute the GET
    // int status = client.executeMethod( get2 );

    // print the status and response
    // System.out.println(status + "\n" + get2.getResponseBodyAsString());

    } finally {
    // release any connection resources used by the method
    get1.releaseConnection();
    }
    }
    }
     
    bcprashanth, Sep 12, 2008 IP