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
Solved

Using httpclient to log into web-mail

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
servlet doXXX methods 3 61
Which non-HTML GUI front end to use with Java? 3 24
eclipse console opening separately 2 29
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

789 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