?
Solved

Using httpclient to log into web-mail

Posted on 2004-09-26
2
Medium Priority
?
512 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

801 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