HttpClient 模拟登陆,保持会话并进行后续操作
2011-03-10 — UnmiApache HttpClient 是很方便的 Java 开源的访问 HTTP 资源的组件。网站上的资源不总是能匿名访问的,很多都需要登陆后才能操作,且不说论坛里登陆后才能发言,就是某些稍显敏感的 XML 等信息也是登陆后才能获取到的。
没问题,HttpClient 能让你做到,它提供了 Basic 和 Form-Based 两种验证方式。登陆后获得服务器端发来的 Cookie 作为下一次访问的凭证, 让服务端认为你还是个合法用户。服务端不是用 Session 来维护会话的吗?是的,Session 也要有个载体,Cookie 了。或有时 Java Web 会用 jsessionid 参数在服务端与客户端来回关联 Session 信息,也没问题,HttpClient 同样能胜任。
下面主要说明 Form-Based 的验证方式,Basic 的验证简单列了几行代码,还未实践,具体可参考文后的链接。
看 Form-Based 方式的演示代码,如果登陆时需要一个验证码的话,那只有自己想办法怎么得到这个码了,登陆时谁都想无码:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
package
cc.unmi.httpclient;
import
org.apache.commons.httpclient.Cookie;
import
org.apache.commons.httpclient.HttpClient;
import
org.apache.commons.httpclient.NameValuePair;
import
org.apache.commons.httpclient.cookie.CookiePolicy;
import
org.apache.commons.httpclient.methods.GetMethod;
import
org.apache.commons.httpclient.methods.PostMethod;
import
org.junit.Test;
public
class
HttpClientLogin {
public
static
void
main(String[] args){
//登陆 Url
String loginUrl =
"http://localhost/unmi/login.html"
;
//需登陆后访问的 Url
String dataUrl =
"http://localhost/unmi/user_info.html?userid=123456"
;
HttpClient httpClient =
new
HttpClient();
//模拟登陆,按实际服务器端要求选用 Post 或 Get 请求方式
PostMethod postMethod =
new
PostMethod(loginUrl);
//设置登陆时要求的信息,一般就用户名和密码,验证码自己处理了
NameValuePair[] data = {
new
NameValuePair(
"username"
,
"Unmi"
),
new
NameValuePair(
"password"
,
"123456"
),
new
NameValuePair(
"code"
,
"anyany"
)
};
postMethod.setRequestBody(data);
try
{
//设置 HttpClient 接收 Cookie,用与浏览器一样的策略
httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
httpClient.executeMethod(postMethod);
//获得登陆后的 Cookie
Cookie[] cookies=httpClient.getState().getCookies();
String tmpcookies=
""
;
for
(Cookie c:cookies){
tmpcookies += c.toString()+
";"
;
}
//进行登陆后的操作
GetMethod getMethod =
new
GetMethod(dataUrl);
//每次访问需授权的网址时需带上前面的 cookie 作为通行证
getMethod.setRequestHeader(
"cookie"
,tmpcookies);
//你还可以通过 PostMethod/GetMethod 设置更多的请求后数据
//例如,referer 从哪里来的,UA 像搜索引擎都会表名自己是谁,无良搜索引擎除外
postMethod.setRequestHeader(
"Referer"
,
"http://unmi.cc"
);
postMethod.setRequestHeader(
"User-Agent"
,
"Unmi Spot"
);
httpClient.executeMethod(getMethod);
//打印出返回数据,检验一下是否成功
String text = getMethod.getResponseBodyAsString();
System.out.println(text);
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
|
Basic 验证的简单代码导引,还未亲试:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
HttpClient client =
new
HttpClient();
// 1
client.getState().setCredentials(
new
AuthScope(
"unmi.cc"
,
80
, AuthScope.ANY_REALM),
new
UsernamePasswordCredentials(
"username"
,
"password"
)
);
// 2
client.getParams().setAuthenticationPreemptive(
true
);
// 3
GetMethod getMothod =
new
GetMethod(
"http://unmi.cc/twitter"
);
// 4
getMothod.setDoAuthentication(
true
);
// 5
int
status = client.executeMethod( getMothod );
|