Solved

Using httpclient to log into web-mail

Posted on 2004-09-26
2
503 Views
Last Modified: 2006-11-17
I've been attempting to use httpclient to log into a web-mail account, however I'm not having any luck getting it to work.  I've tried following the resources at http://cvs.apache.org/viewcvs.cgi/jakarta-commons/httpclient/src/examples/?only_with_tag=HTTPCLIENT_2_0_BRANCH, but I think I'm just not understanding it.  I've downloaded Mozilla and installed the firebird plugin to allow me to spy on the headers sent to and from the web browser to see what parameters need to be passed as part of the post, but as I said I think I'm just a lost duck in this project so far.

Here's my code so far with the results below:

package MailRU;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;
import java.io.*;


public class HttpClientTest
{
   public static void main(String[] args)
   {
      PostMethod post = null;
      try
      {

         HttpState initialState = new HttpState();

         //--- add some cookies (host, cookie name, cookie value, path)
//         Cookie mycookie = new Cookie("localhost", "mycookie", "stuff", "/", null, false);
//         initialState.addCookie(mycookie);

         //--- create instance and set cookie state
         HttpClient httpclient = new HttpClient();
         httpclient.setState(initialState);
         
         post = new PostMethod("http://www.mail.ru");
         int responseCode = httpclient.executeMethod(post);
         
         Cookie[] cookies = httpclient.getState().getCookies();

         //--- Display all the current cookies
         System.out.println("Present cookies: ");

         for (int i = 0; i < cookies.length; i++)
         {
            System.out.println(" - " + cookies[i].toExternalForm());
         }
         
         String sessionCookie = cookies[0].toString();
         System.out.println(sessionCookie);
           
         post = new PostMethod("http://www.mail.ru/");
                 
         post.setFollowRedirects(true);
         responseCode = httpclient.executeMethod(post);

         System.out.println("response code was [" + responseCode + "]");

//         System.out.println("response body is [\n" + post.getResponseBodyAsString() + "\n]");
         
         String encoding = "UTF8";
         BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c://x.htm"), encoding));
         try{
            out.write(post.getResponseBodyAsString());
            out.flush();
         }
         catch(Exception e){
             e.printStackTrace();
         }
         
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
      finally
      {
         //--- cleanup...
         post.releaseConnection();
      }
   }
}


RESULTS:

Present cookies:
 - Mpopl=1780055255
Mpopl=1780055255
response code was [200]


The page written to x.htm was the login page again.  I didn't pass the login parameters so this is what would be expected, but this is what I'm having a hard time doing.  I've tried using

post.addParameter("Login", "usernamehere");
post.addParameter("Password", "passwordhere");

Apparantly I'm not formatting the response correctly or not supplying everything required, I'm not sure.  If you need to see the headers from my browser, I can post them as well.

P.S  I've been working at this for about a week and I'm about to pull out all my hair.  haha

Thanks,
David
0
Comment
Question by:lomidien
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12156741
the login request should be to the url:

http://win.mail.ru/cgi-bin/auth

and you need to set the follwoing request parameters:

Mpopl
Login
Domain
Password
level

The value for Mpopl will need to be extracted from html page.
0
 

Author Comment

by:lomidien
ID: 12156782
objects,

Perfect!  Turns out I didn't need level, but it returned the page just perfect.  I'm going to open a new question to ask you (if you would be kind enough to help out of course) about navigating from that page (which shows how many messages are in the inbox) to the inbox page itself so that I can parse the text for particular values and send myself an alarm if certain conditions are met.  I assume this has to be done with a cookie being set so that it keeps the sessions correct, but I'm not sure.

Thanks again!!
David
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question