Skip to content

Commit 34232bc

Browse files
committed
更新版本beta-1.1.0;添加HttpCookies类,用于简化Cookie操作,附一个示例,见TestCookieWithHttpCookies.java
1 parent b8c2229 commit 34232bc

3 files changed

Lines changed: 155 additions & 1 deletion

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.tgb.ccl.http</groupId>
55
<artifactId>httpclientUtil</artifactId>
6-
<version>1.0.0-beta</version>
6+
<version>1.1.0-beta</version>
77
<description>基于HttpClient-4.4.1、HttpAsyncClient-4.1封装的工具类。支持插件式配置Header、插件式配置httpclient对象,这样就可以方便地自定义header信息、配置ssl、配置proxy、Cookie等</description>
88

99
<properties>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.tgb.ccl.http.common;
2+
3+
import org.apache.http.client.CookieStore;
4+
import org.apache.http.client.protocol.HttpClientContext;
5+
import org.apache.http.impl.client.BasicCookieStore;
6+
7+
/**
8+
* 封装Cookie
9+
*
10+
* @author arron
11+
* @date 2016年1月12日 上午8:42:13
12+
* @version 1.0
13+
*/
14+
public class HttpCookies {
15+
16+
/**
17+
* 使用httpcontext,用于设置和携带Cookie
18+
*/
19+
private HttpClientContext context ;
20+
21+
/**
22+
* 储存Cookie
23+
*/
24+
private CookieStore cookieStore;
25+
26+
public static HttpCookies custom(){
27+
return new HttpCookies();
28+
}
29+
30+
private HttpCookies(){
31+
this.context = new HttpClientContext();
32+
this.cookieStore = new BasicCookieStore();
33+
this.context.setCookieStore(cookieStore);
34+
}
35+
36+
public HttpClientContext getContext() {
37+
return context;
38+
}
39+
40+
public HttpCookies setContext(HttpClientContext context) {
41+
this.context = context;
42+
return this;
43+
}
44+
45+
public CookieStore getCookieStore() {
46+
return cookieStore;
47+
}
48+
49+
public HttpCookies setCookieStore(CookieStore cookieStore) {
50+
this.cookieStore = cookieStore;
51+
return this;
52+
}
53+
54+
55+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.tgb.ccl.http;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
10+
import org.apache.http.Header;
11+
12+
import com.tgb.ccl.http.common.HttpCookies;
13+
import com.tgb.ccl.http.common.HttpHeader;
14+
import com.tgb.ccl.http.exception.HttpProcessException;
15+
import com.tgb.ccl.http.httpclient.HttpClientUtil;
16+
17+
/**
18+
* 测试携带cookie的操作(使用HttpCookies)
19+
*
20+
* @author arron
21+
* @date 2016年1月12日 下午2:15:17
22+
* @version 1.0
23+
*/
24+
public class TestCookieWithHttpCookies {
25+
26+
public static void main(String[] args) throws HttpProcessException {
27+
//登录地址
28+
String loginUrl = "https://passport.csdn.net/account/login";
29+
//C币查询
30+
String scoreUrl = "http://my.csdn.net/my/score";
31+
32+
HttpCookies cookies = HttpCookies.custom();
33+
34+
//获取参数
35+
String loginform = HttpClientUtil.send(loginUrl, cookies.getContext());
36+
//System.out.println(loginform);
37+
38+
// //打印参数,可以看到cookie里已经有值了。
39+
// for (Cookie cookie : cookies.getCookieStore().getCookies()) {
40+
// System.out.println(cookie.getName()+"--"+cookie.getValue());
41+
// }
42+
43+
System.out.println("获取登录所需参数");
44+
String lt = regex("\"lt\" value=\"([^\"]*)\"", loginform)[0];
45+
String execution = regex("\"execution\" value=\"([^\"]*)\"", loginform)[0];
46+
String _eventId = regex("\"_eventId\" value=\"([^\"]*)\"", loginform)[0];
47+
48+
//组装参数
49+
Map<String, Object> map = new HashMap<String, Object>();
50+
map.put("username", "用户名");
51+
map.put("password", "密码");
52+
map.put("lt", lt);
53+
map.put("execution", execution);
54+
map.put("_eventId", _eventId);
55+
56+
//发送登录请求
57+
String result = HttpClientUtil.send(loginUrl, map, cookies.getContext());
58+
//System.out.println(result);
59+
if(result.contains("帐号登录")){//如果有帐号登录,则说明未登录成功
60+
String errmsg = regex("\"error-message\">([^<]*)<", result)[0];
61+
System.err.println("登录失败:"+errmsg);
62+
return;
63+
}
64+
System.out.println("----登录成功----");
65+
66+
// //打印参数,可以看到cookie里已经有值了。
67+
// for (Cookie cookie : cookies.getCookieStore().getCookies()) {
68+
// System.out.println(cookie.getName()+"--"+cookie.getValue());
69+
// }
70+
71+
//访问积分管理页面
72+
Header[] headers = HttpHeader.custom().userAgent("User-Agent: Mozilla/5.0").build();
73+
result = HttpClientUtil.send(scoreUrl, headers, cookies.getContext());
74+
//获取C币
75+
String score = regex("\"last-img\"><span>([^<]*)<", result)[0];
76+
System.out.println("您当前有C币:"+score);
77+
78+
}
79+
80+
81+
/**
82+
* 通过正则表达式获取内容
83+
*
84+
* @param regex 正则表达式
85+
* @param from 原字符串
86+
* @return
87+
*/
88+
public static String[] regex(String regex, String from){
89+
Pattern pattern = Pattern.compile(regex);
90+
Matcher matcher = pattern.matcher(from);
91+
List<String> results = new ArrayList<String>();
92+
while(matcher.find()){
93+
for (int i = 0; i < matcher.groupCount(); i++) {
94+
results.add(matcher.group(i+1));
95+
}
96+
}
97+
return results.toArray(new String[]{});
98+
}
99+
}

0 commit comments

Comments
 (0)