Solved

Using httpclient to log into web-mail

Posted on 2004-09-26
2
491 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now