为什么HttpClient在执行post方法后会停留在登录页面上?

时间:2011-08-19 09:09:40

标签: java login httpclient

我尝试使用HttpClient登录此网站:

http://softmanage.hengxd.com/index.asp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/base.css" rel="stylesheet" type="text/css" />

<title>Application Management Platform</title>
<style type="text/css">
<!--
.STYLE1 {
    font-size: 16px;
    font-weight: bold;
}
-->
</style>
</head>

<script>
<!--
function OnSubmitForm()
{
    if(document.getElementById("username").value=="")
    {
        alert("username must not be empty");
        document.getElementById("username").focus();
        return;
    }
    if(document.getElementById("userpass").value=="")
    {
        alert("password must not be empty");
        document.getElementById("userpass").focus();
        return;
    }
    document.getElementById("lform").submit();
}
-->
</script>
<body >
<table width="100%" height="500" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" valign="middle">
    <form name="lform" id="lform" action="index.asp?action=1" method="post" style="padding:0; margin:0">
    <div style="width:416px; height:229px; background:url(images/lbg.jpg) center no-repeat;">
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td height="40" colspan="2" align="center"><span class="STYLE1">Login</span></td>
          </tr>
        <tr>
          <td width="35%" height="58" align="right">Username:</td>
          <td width="65%" height="58" align="left"><input type="text" name="username" id="username" /></td>
        </tr>
        <tr>
          <td height="58" align="right">Password:</td>
          <td height="58" align="left"><input type="password" name="userpass" id="userpass" /></td>
        </tr>
        <tr>
          <td colspan="2" align="center"><img src="images/lbt.jpg" onclick="OnSubmitForm();"/></td>
          </tr>
      </table>
    </div>
    </form>
    </td>
  </tr>
</table>
</body>
</html>

要登录此站点,我使用了以下java代码:

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class HttpClientText {

    public static void main(String [] args) throws Exception {

        HttpClient client = new HttpClient();
        client.getHostConfiguration().setHost("http://softmanage.hengxd.com/index.asp", 8081);

        PostMethod post = new PostMethod("http://softmanage.hengxd.com/index.asp");

        NameValuePair username = new NameValuePair("username", "test");
        NameValuePair password = new NameValuePair("userpass", "12345"); 

        post.setRequestBody(new NameValuePair[] { username, password });

        client.executeMethod(post);

        String responseString = new String(post.getResponseBodyAsString().getBytes("utf-8"));

        System.out.println(responseString);
    }
}

我希望它在登录后打印出页面,但事实上它仍打印出原始页面,这是我上面列出的html。谁能找到这个问题的答案?谢谢!

1 个答案:

答案 0 :(得分:2)

表格说action="index.asp?action=1。如果我使用它,我会重定向到lansel.asp。

        PostMethod post = new PostMethod("http://softmanage.hengxd.com/index.asp?action=1");