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登录吧!
其实一般的实质性网站都是一样的操作步骤,只要找对相应的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登录吧!
相关文章推荐
- C++使用libcurl做HttpClient
- 在 .NET Core 中结合 HttpClientFactory 使用 Polly(上篇)
- HttpClient使用之下载远程服务器中的文件(注意目录遍历漏洞)
- 使用天气预报api(中国天气网)获取城市天气(2)---httpclient和json操作
- HttpClient和HtmlUnit的比较总结以及使用技巧(一)
- HttpClient androidStudio中的基础使用
- HttpClient androidStudio中的基础使用
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- HttpClient使用当中需要主要的地方
- HttpClient使用cookie
- AndroidHttpClient使用Cookie应用分析
- Apache Common HttpClient使用之七种武器
- 使用 HTTP 协议访问网络的两种方式:HttpURLConnection 和 HttpClient
- 安卓使用OkHttpClient进行网络请求
- Android访问网络,使用HttpURLConnection还是HttpClient?
- 使用 TRESTClient 与 TRESTRequest 作为 HTTP Client
- Windows Phone 7 网络编程之webclient和httpwebrequest的使用
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
- 使用POST、GET、AsyncHttpClient创造服务端用手机端来连接进行登录
- HttpClient, 使用C#操作Web