您的位置:首页 > 理论基础 > 计算机网络

httpclient 使用 痛苦历程

2010-06-23 16:04 375 查看
开始学习Httpclient 模拟登录一般的小网站时,都是非常容易,当Boss发下来的任务为 登录各大招聘网显示岗位发布

其实一般的实质性网站都是一样的操作步骤,只要找对相应的post 参数和 get 路径 保妳模拟有效。

但遇到的问题也不是相信的能解决的。! 往往在登录的过程中出现登录不成功这就是卡时间的问题了,因为找到一个相应的问题所需

要时间很长,在这里我提供一些相应的实施案例吧!

1。但一个网站登录成功 且无法跳转,这时候需要检查的是client策略、Cookie问题、和设置重定向

来看看吧。比如说智联登录

/**
* 测试登录
* @return
*/
public static String login(){
String url="";
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT,"http"); //加载
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); //设置编号
client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY); //设置流量器格式
PostMethod post = new PostMethod("http://rd2.zhaopin.com/loginmgr/loginproc.asp"); //登录网址
NameValuePair user = new NameValuePair("username","xx"); //账号密码
NameValuePair pwd = new NameValuePair("password","xx");

try{
post.setRequestBody(new NameValuePair[]{user,pwd}); //设置post参数
int status=client.executeMethod(post); //执行post
Header[] hd=post.getResponseHeaders(); //登录跳转的location
for(Header dd: hd){
System.out.println(dd.getName()+":"+dd.getValue());
}
Header location=post.getResponseHeader("location");
GetMethod get=new GetMethod(location.getValue());
//登录成功之后跳转
get.setFollowRedirects(true);
get.getParams().setParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true);
int status2=client.executeMethod(get);
//登录跳转的location

//System.out.println(get.getResponseBodyAsString());
get.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}

post.releaseConnection();
return url;
}

登录成功无跳转修正!

2.在来个Httpclient ajax登录吧!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: