利用http消息头做重放的功能

wxl1214471 2013-01-28 09:54:41
我想做这样一个功能,有一个<a href="xx.action">xxx网站</a>,当我点击时,携带着用户名和密码去请求"xxx网站",(当然,用户名和密码一定是这个网站里存在的),如果用户名和密码正确的话,就跳转到xxx网站,要求是登录后的状态,而不是跳到登录页面。
我的思路是利用HttpClient携带username和password访问xxx网站,但返回的是登录后的Body代码,而不是登录后的页面,我的代码出下:请高手帮忙。
public String jumpInstruction() throws Exception {
httpClient = new HttpClient();
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
try {
String newUrl = "http://192.168.1.10/wiki/index.php?user-login";
String Referer = " http://192.168.1.10/wiki/index.php?user-login";
String ContentType = "application/x-www-form-urlencoded";
postMethod = new PostMethod(newUrl);
postMethod.setRequestHeader("Referer", Referer);
postMethod.setRequestHeader("Content-Type", ContentType);
NameValuePair[] data = {
new NameValuePair("username","wxl"),
new NameValuePair("password","wxl"),
new NameValuePair("submit","%E7%99%BB%E5%BD%95")// 这里我用FireFox做重放时,里面只要有newUrl、Referer、ContentType这三项就可以重放登录成功
};
postMethod.setRequestBody(data);
int code = httpClient.executeMethod(postMethod);
Cookie[] cookies = httpClient.getState().getCookies();
for(Cookie obj : cookies){
httpClient.getState().addCookies(cookies);
System.out.println(obj.getName() + "=" + obj.getValue());// 这里Cookie可以正常写入
}
System.out.println(cookies.length + " cookie.length-----"); // 这里返回的始终是200,不知道为什么?

} catch (HttpException e) {
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放连接
postMethod.releaseConnection();
}
return null;
}
...全文
44 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧