开发者

Why HttpClient stays on the login page after executing post method?

开发者 https://www.devze.com 2023-03-29 10:15 出处:网络
I am try to use HttpClient to login this website: http://softmanage.hengxd.com/index.asp <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-

I am try to use HttpClient to login this website:

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;
    }
    开发者_Python百科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>

To log in this site, I used the following java code:

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);
    }
}

I expect it to print out the page after logged in, but in fact it still prints out the original page, which is the html I listed above. Could anyone find an answer to this question? Thanks!


The Form says action="index.asp?action=1. If I use that i get a redirect to lansel.asp.

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

精彩评论

暂无评论...
验证码 换一张
取 消