Results 1 to 4 of 4
  1. #1
    forrani is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Smile How to login to email(web.de) with httpclient in java

    Hello Friends,
    I am new to java as well as httpclient. I am trying to login to an email account(forragini@web.de) with httpclient in java but not able to do so. Here is my java code. I would really appreciate any help...

    import java.io.DataOutputStream;
    import java.io.FileOutputStream;
    import org.apache.commons.httpclient.cookie.CookiePolicy;
    import org.apache.commons.httpclient.cookie.CookieSpec;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.commons.httpclient.params.HttpMethodPar ams;
    import org.apache.commons.httpclient.Cookie;
    import org.apache.commons.httpclient.Header;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpConnection;
    import org.apache.commons.httpclient.HttpState;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.NameValuePair;

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

    HttpClient client = new HttpClient();

    HttpState initialState = new HttpState();
    client.getParams().setParameter(HttpMethodParams.U SER_AGENT,
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)");


    client.getParams().setCookiePolicy(CookiePolicy.BR OWSER_COMPATIBILITY);


    GetMethod get = new GetMethod("http://www.web.de/fm/");
    // GetMethod get = new GetMethod("http://web.de/fm_nossl/");
    //HttpGet get = new HttpGet("http://www.web.de/fm/");

    client.executeMethod(get);

    System.out.println("Login form get: " + get.getStatusLine().toString());

    Cookie[] get_cookies = client.getState().getCookies();

    if (get_cookies.length == 0)
    {
    System.out.println("No cookies after GET Method !");
    }
    else
    {
    for (int i = 0; i < get_cookies.length; i++)
    {
    System.out.println("- " + get_cookies[i].toString());
    }
    }

    get.releaseConnection();


    PostMethod post = new PostMethod("https://login.web.de/intern/login/");

    post.getParams().setCookiePolicy(CookiePolicy.BROW SER_COMPATIBILITY);


    //HttpPost post = new HttpPost("https://login.web.de/intern/login/");

    NameValuePair userid = new NameValuePair("username", "myemailid");
    NameValuePair password = new NameValuePair("password", "mypwd");
    NameValuePair button = new NameValuePair("rv_dologon", "Login");
    NameValuePair hidden = new NameValuePair("uinguserid", "ac140872-4277-1283513089-3");
    NameValuePair[] data = {userid,password,button,hidden};

    //post.addParameters(data);
    post.setRequestBody(data);
    client.executeMethod(post);
    //post.setFollowRedirects(true);


    System.out.println("Login form post: " + post.getStatusLine().toString());


    Cookie[] get_cookies1 = client.getState().getCookies();

    if (get_cookies1.length == 0)
    {
    System.out.println("No cookies after POST Method !");
    }
    else
    {
    for (int i = 0; i < get_cookies1.length; i++)
    {
    System.out.println("- " + get_cookies1[i].toString());
    }
    }

    post.releaseConnection();

    int statuscode = post.getStatusCode();
    if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
    (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
    (statuscode == HttpStatus.SC_SEE_OTHER) ||
    (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT))
    {
    Header header = post.getResponseHeader("Location");


    System.out.println("Header is : " + header.getValue());

    if (header != null)
    {
    String newuri = header.getValue();

    System.out.println("Redirect target: " + newuri);
    GetMethod redirect = new GetMethod(newuri);
    int code = client.executeMethod(redirect);
    System.out.println("Status code of Redirect method is :"+ code);
    System.out.println("Redirect: " + redirect.getStatusLine().toString());



    String outputpage = redirect.getResponseBodyAsString();


    FileOutputStream fos= new FileOutputStream("C:\\Documents and Settings\\patel\\My Documents\\sourcecodeemail.html");
    DataOutputStream dos = new DataOutputStream(fos);
    dos.writeChars(outputpage);

    Cookie[] get_cookies11 = client.getState().getCookies();

    if (get_cookies11.length == 0)
    {
    System.out.println("No cookies");
    }
    else
    {
    for (int i = 0; i < get_cookies11.length; i++)
    {
    System.out.println("- " + get_cookies11[i].toString());
    }
    }


    redirect.releaseConnection();

    }
    else
    {
    System.out.println("Invalid redirect");

    }


    }

    }
    }


    Thanks in advance..

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    it's possible the contents of the POST are incomplete, for example, some sites now use JavaScript to append additional values to a HTML form after it loads onto the screen (a dynamic html kind of thing). If you have the firebug plugin for and/or "HttpFox" plugins for Firefox, experiment with and observe the cookies and the net activity as you perform a manual login thru Firefox.

  3. #3
    forrani is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    @travishein
    Thank you very much for ur reply..I have installed add on for jsp. If I disallow scripts globally then also I am able to login to web.de through mozilla browser. So I guess it is not using any jsp. Also I tried with "tamper" to login from browser. In that i noticed that it sends the same data through POST which I am passing in my code. By this I concluded that parameters I am passing is correct. and as far as cookies are concerned I dont get any cookies in my output. Also if I disable cookies I am able to login to the email account from the browser. Now if u have any other idea regarding this please let me know. I would really appreciate that. I am stucked here and dont know what to try next !!

    Regards..

    Rani

  4. #4
    forrani is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    @travishein

    Hi..I just got it done... You were right...I was not submitting some hidden data in POST Method..I passed them and I got it done...

    Thanks again...

Similar Threads

  1. login with cookies using Apache httpClient
    By MacGyver in forum Advanced Java
    Replies: 0
    Last Post: 05-15-2010, 03:21 PM
  2. Using HttpClient
    By jdetloff in forum New To Java
    Replies: 4
    Last Post: 01-06-2010, 10:43 AM
  3. send email using apache commons email
    By jnamendi in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 10-14-2008, 05:55 PM
  4. Replies: 0
    Last Post: 06-25-2008, 12:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •