Solved

Using httpclient to log into web-mail

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

696 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