有关HttpClient模拟用户登录问题,网上资料介绍很多,在我要登录的站点是用Acegi控制权限的,测试多次都不成功,不知道什么原因。
Souce Code:
HttpClient client = new HttpClient();
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
try {
PostMethod post = new PostMethod("http://localhost:8080/test/");
NameValuePair name = new NameValuePair("j_username", "aaa");
NameValuePair pass = new NameValuePair("j_password", "111");
post.setRequestBody(new NameValuePair[]{name,pass});
int status = client.executeMethod(post);
System.out.println(status);
System.out.println(post.getResponseBodyAsString()); // 得到的还是登录页面的内容
post.releaseConnection();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}