How to login a page with httpclient API?
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">
<!-- =EF=BF=BD -->
<meta http-equiv="Content-type" content="text/html; charset=euc-jp" />=
<title>SquirrelMail - =E3=83=AD=E3=82=B0=E3=82=A4=E3=83=B3</title><script la=
nguage="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[i].type == "text" ||
document.forms[0].elements[i].type == "password") {
textElements++;
if (textElements == 1) {
document.forms[0].elements[i].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 =E3=83=AD=E3=82=B4" width="308" height==
"111" /><br />
<small>SquirrelMail =E3=83=90=E3=83=BC=E3=82=B8=E3=83=A7=E3=83=B3 1.4.8<br /=
SquirrelMail =E3=83=97=E3=83=AD=E3=82=B8=E3=82=A7=E3=82=AF=E3=83=88=E3=81=
=AE=E3=83=81=E3=83=BC=E3=83 =E3=81=AB=E3=82=88=E3=81=A3=E3=81=A6<br /></sm=
all>
<table bgcolor="#ffffff" border="0" width="350"><tr><td
bgcolor="#dcdcdc" align="center"><b>SquirrelMail =E3=83=AD=E3=82=B0=E3=
=82=A4=E3=83=B3</b>
</td>
</tr>
<tr><td bgcolor="#ffffff" align="left">
<table bgcolor="#ffffff" align="center" border="0"
width="100%"><tr><td align="right" width="30%">=E3=83=AD=E3=82=B0=E3=
=82=A4=E3=83=B3=E5=90=8D=EF=BC=9A</td>
<td align="left" width="*"><input type="text" name="login_username"
value="" />
</td>
</tr>
<tr><td align="right" width="30%">=E3=83=91=E3=82=B9=E3=83=AF=E3=83=BC=
=E3=83=89=EF=BC=9A</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="=E3=83=AD=E3=
=82=B0=E3=82=A4=E3=83=B3" />
</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