How to login to the webpage with httpclient API

N

ngxfer001

Hi Experts,

I am new the httpclient API. I am trying to access a webmail and grap
the email from the webmail. I found out that httpclient API can do
that, the web page login page is as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>
<meta name="robots" content="noindex,nofollow">
<!-- � -->
<meta http-equiv="Content-type" content="text/html; charset=euc-jp" />

<title>SquirrelMail - ログイン</title><script language="JavaScript"
type="text/javascript">
<!--
function squirrelmail_loginpage_onload() {
document.forms[0].js_autodetect_results.value = '1';
var textElements = 0;
for (i = 0; i < document.forms[0].elements.length; i++) {
if (document.forms[0].elements.type == "text" ||
document.forms[0].elements.type == "password") {
textElements++;
if (textElements == 1) {
document.forms[0].elements.focus();
break;
}
}
}
}
// -->
</script>

<!--[if IE 6]>
<style type="text/css">
/* avoid stupid IE6 bug with frames and scrollbars */
body {
width: expression(document.documentElement.clientWidth - 30);
}
</style>
<![endif]-->

</head>

<body text="#000000" bgcolor="#ffffff" link="#0000cc" vlink="#0000cc"
alink="#0000cc" onLoad="squirrelmail_loginpage_onload();">
<form action="redirect.php" method="post">
<table bgcolor="#ffffff" border="0" cellspacing="0" cellpadding="0"
width="100%"><tr><td align="center"><center><img src="../images/
sm_logo.png" alt="SquirrelMail ロゴ" width="308" height="111" /><br />
<small>SquirrelMail ãƒãƒ¼ã‚¸ãƒ§ãƒ³ 1.4.8<br />
SquirrelMail プロジェクトã®ãƒãƒ¼ãƒ ã«ã‚ˆã£ã¦<br /></small>
<table bgcolor="#ffffff" border="0" width="350"><tr><td
bgcolor="#dcdcdc" align="center"><b>SquirrelMail ログイン</b>
</td>
</tr>
<tr><td bgcolor="#ffffff" align="left">
<table bgcolor="#ffffff" align="center" border="0"
width="100%"><tr><td align="right" width="30%">ログインå:</td>
<td align="left" width="*"><input type="text" name="login_username"
value="" />
</td>

</tr>

<tr><td align="right" width="30%">パスワード:</td>
<td align="left" width="*"><input type="password" name="secretkey" />
<input type="hidden" name="js_autodetect_results" value="0" />
<input type="hidden" name="just_logged_in" value="1" />
</td>
</tr>
</table>
</td>
</tr>
<tr><td align="left"><center><input type="submit" value="ログイン" />
</center></td>
</tr>
</table>
</center></td>

</tr>
</table>
</form>
</body></html>


What I want to do is to put the username and password in the webpage
and click submit

My code is as follows:

public static void main(String[] args) throws Exception
{
HttpClient client = new HttpClient();

PostMethod authpost = new PostMethod(FRONT_PAGE);
NameValuePair usr = new NameValuePair("login_username",
username);
NameValuePair psw = new NameValuePair("secretkey", password);

authpost.setRequestBody(new NameValuePair[] {usr, psw,
action});
client.executeMethod(authpost);

System.out.println("Login form post: " +
authpost.getStatusLine().toString());

System.out.println(authpost.getResponseBodyAsString());

authpost.releaseConnection();

}

I know that this doesn't work because of the following question?
1) How to click the button with httpclient API?
2) I feel that the javascript will affect my login?

Can you please help me, I try to find in google for 2 days and try
many example and it doesn't work.

Thank you for reading this post.

best Regards
Ferdinand Ng
 
N

ngxfer001

Hi RGB,

Thank you very much for your answer, I was almost lost hope that no
one will reply me.

On the question, Is FRONT_PAGE = siteName + "redirect.php"?
It is actually the address of the login page. Is it alright?

public static void main(String[] args) throws Exception
{
HttpClient client = new HttpClient();

PostMethod authpost = new PostMethod(FRONT_PAGE);

NameValuePair usr = new NameValuePair("login_username",
username);
NameValuePair psw = new NameValuePair("secretkey", password);
NameValuePair autodetect = new
NameValuePair("js_autodetect_results", "0");
NameValuePair login = new NameValuePair("just_logged_in",
"1");

authpost.setRequestBody(new NameValuePair[] {usr, psw,
autodetect, login});
client.executeMethod(authpost);

System.out.println("Login form post: " +
authpost.getStatusLine().toString());
System.out.println(authpost.getResponseBodyAsString());

authpost.releaseConnection();

}

I put the two hidden field in, I was expecting that the
getResponseBodyAsString will return the page after login, but it
return the page at the login page, doesn't seems to send request to
the server? am I do it right?

I am installing the WireShark, I will check it now, will put the
result back here.

For more information, the page is a squirrelmail which provide by my
company to allows us to check email there.

The reason I am doing this is that there are many email (same format)
contains data in it, currently, I am doing it manually by opening the
email and convert it to excel, I would like to write an application to
automate the process.

I really appreciate your help.

Best Regard
Ferdinand Ng
 
A

Andrew Thompson

On May 28, 1:54 am, (e-mail address removed) wrote:
...
Thank you very much for your answer, I was almost lost hope that no
one will reply me.

After just 5 hours? That is very little hope.
 
N

ngxfer001

Hi Andrew,

hahaha, somehow is strange, this page probably shows the time in the
user time zone, cause I post it last mid nite, and was checking the
whole day without answer, only like few hours ago, thanks to RGB for
his answer. Maybe I am wrong, I guess the time is the time in the user
time zone. So the 5 hours different here might be 5 hours + time
difference between me and RGB region. ^.^

Thanks for your reply.

Best Regards
Ferdinand Ng
 
N

ngxfer001

Sorry, some additional information,

the page I am trying to login in https? will that make a difference?

Best Regards
Ferdinand Ng
 
D

Donkey Hot

(e-mail address removed) wrote in @d19g2000prm.googlegroups.com:
Sorry, some additional information,

the page I am trying to login in https? will that make a difference?

Best Regards
Ferdinand Ng

No.

But what makes a difference, is the page your are going to access. It is
not the FRONTPAGE, but the page meantioned in the Form's action property.

It was something like "redirect.php" it I recall right.

You need to POST the Form to that address, with all the elements in it.
Username, password, including also all hidden elements.

Your mission is not easy. I would never even try to do what you are
trying... But that's me;) Lazy bastard me.

You can use the html-parser to get the Form elements, or you can just look
at the front page to get and hard code them. But in later screens, there
will be much more elements to deal with, and the html-parser will be handy.

The login page is pretty simple and easy.
 
N

ngxfer001

Dear RGB,

Thanks for all the valuable advice you gave me, I have been
successfully login the page. Since I am not a web developer, so I
actually not familiar with web technology like PHP or HTML. When I
login, I got the following page

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html><head>
<meta name="robots" content="noindex,nofollow">
<title>SquirrelMail 1.4.8</title>
</head><frameset cols="150, *" id="fs1">
<frame src="left_main.php" name="left" frameborder="1" />
<frame src="right_main.php" name="right" frameborder="1" />
</frameset>
</html>

I login to the webmail with Firefox and look at the source, it is the
same.

Just to describe a bit visually, it has two panel, left and right,
left is list out the options, like inbox, outbox etc. on the right
side is the mail list.I suppose I will have to go further inside the
right_main.php. I will update this thread accordingly just to keep
record for future reference.

The code that I used to login now is as follows:

public static void main(String[] args) throws Exception
{
HttpClient client = new HttpClient();
//client.getHostConfiguration().setHost(FRONT_PAGE,
LOGON_PORT, "https");

PostMethod authpost = new PostMethod(FRONT_PAGE +
"redirect.php");

NameValuePair usr = new NameValuePair("login_username",
username);
NameValuePair psw = new NameValuePair("secretkey", password);
NameValuePair autodetect = new
NameValuePair("js_autodetect_results", "0");
NameValuePair login = new NameValuePair("just_logged_in",
"1");
authpost.setRequestBody(new NameValuePair[] {usr, psw,
autodetect, login});

client.executeMethod(authpost);

String redirectlink =
authpost.getResponseHeader("location").getValue();
System.out.println("Login form post: " +
authpost.getStatusLine().toString());
System.out.println(authpost.getResponseHeader("location"));
authpost.releaseConnection();

GetMethod authget = new GetMethod(FRONT_PAGE + redirectlink);
client.executeMethod(authget);
System.out.println("Login form post: " +
authget.getStatusLine().toString());
System.out.println(authget.getResponseBodyAsString());
}

Thanks alot for your help, RGB. I might need your help after this
step, but I hope I can crack all the problem myself. Thanks alot
again.

Best regards
Ferdinand Ng
 
N

ngxfer001

Dear RGB,

Thanks for all the valuable advice you gave me, I have been
successfully login the page. Since I am not a web developer, so I
actually not familiar with web technology like PHP or HTML. When I
login, I got the following page

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html><head>
<meta name="robots" content="noindex,nofollow">
<title>SquirrelMail 1.4.8</title>
</head><frameset cols="150, *" id="fs1">
<frame src="left_main.php" name="left" frameborder="1" />
<frame src="right_main.php" name="right" frameborder="1" />
</frameset>
</html>

I login to the webmail with Firefox and look at the source, it is the
same.

Just to describe a bit visually, it has two panel, left and right,
left is list out the options, like inbox, outbox etc. on the right
side is the mail list.I suppose I will have to go further inside the
right_main.php. I will update this thread accordingly just to keep
record for future reference.

The code that I used to login now is as follows:

public static void main(String[] args) throws Exception
{
HttpClient client = new HttpClient();
//client.getHostConfiguration().setHost(FRONT_PAGE,
LOGON_PORT, "https");

PostMethod authpost = new PostMethod(FRONT_PAGE +
"redirect.php");

NameValuePair usr = new NameValuePair("login_username",
username);
NameValuePair psw = new NameValuePair("secretkey", password);
NameValuePair autodetect = new
NameValuePair("js_autodetect_results", "0");
NameValuePair login = new NameValuePair("just_logged_in",
"1");
authpost.setRequestBody(new NameValuePair[] {usr, psw,
autodetect, login});

client.executeMethod(authpost);

String redirectlink =
authpost.getResponseHeader("location").getValue();
System.out.println("Login form post: " +
authpost.getStatusLine().toString());
System.out.println(authpost.getResponseHeader("location"));
authpost.releaseConnection();

GetMethod authget = new GetMethod(FRONT_PAGE + redirectlink);
client.executeMethod(authget);
System.out.println("Login form post: " +
authget.getStatusLine().toString());
System.out.println(authget.getResponseBodyAsString());
}

Thanks alot for your help, RGB. I might need your help after this
step, but I hope I can crack all the problem myself. Thanks alot
again.

Best regards
Ferdinand Ng
 
N

ngxfer001

Dear RGB,

Thanks for all the valuable advice you gave me, I have been
successfully login the page. Since I am not a web developer, so I
actually not familiar with web technology like PHP or HTML. When I
login, I got the following page

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html><head>
<meta name="robots" content="noindex,nofollow">
<title>SquirrelMail 1.4.8</title>
</head><frameset cols="150, *" id="fs1">
<frame src="left_main.php" name="left" frameborder="1" />
<frame src="right_main.php" name="right" frameborder="1" />
</frameset>
</html>

I login to the webmail with Firefox and look at the source, it is the
same.

Just to describe a bit visually, it has two panel, left and right,
left is list out the options, like inbox, outbox etc. on the right
side is the mail list.I suppose I will have to go further inside the
right_main.php. I will update this thread accordingly just to keep
record for future reference.

The code that I used to login now is as follows:

public static void main(String[] args) throws Exception
{
HttpClient client = new HttpClient();
//client.getHostConfiguration().setHost(FRONT_PAGE,
LOGON_PORT, "https");

PostMethod authpost = new PostMethod(FRONT_PAGE +
"redirect.php");

NameValuePair usr = new NameValuePair("login_username",
username);
NameValuePair psw = new NameValuePair("secretkey", password);
NameValuePair autodetect = new
NameValuePair("js_autodetect_results", "0");
NameValuePair login = new NameValuePair("just_logged_in",
"1");
authpost.setRequestBody(new NameValuePair[] {usr, psw,
autodetect, login});

client.executeMethod(authpost);

String redirectlink =
authpost.getResponseHeader("location").getValue();
System.out.println("Login form post: " +
authpost.getStatusLine().toString());
System.out.println(authpost.getResponseHeader("location"));
authpost.releaseConnection();

GetMethod authget = new GetMethod(FRONT_PAGE + redirectlink);
client.executeMethod(authget);
System.out.println("Login form post: " +
authget.getStatusLine().toString());
System.out.println(authget.getResponseBodyAsString());
}

Thanks alot for your help, RGB. I might need your help after this
step, but I hope I can crack all the problem myself. Thanks alot
again.

Best regards
Ferdinand Ng
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top