对httpclient4.3版本的一个简单封装,下面是代码
1
2
3
4
5
6
7
8
9
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/**
* httputil工具类
*
* @author rex
*/
public
class
HttpUtil {
private
static
CloseableHttpClient client;
private
static
BasicCookieStore cookieStore;
private
static
HttpGet get;
private
static
HttpPost post;
private
static
HttpResponse response;
private
static
Result result;
private
static
HttpEntity entity;
/**
* //TODO get请求
*
* @param url 请求地址
* @param headers 请求头
* @param params 请求参数
* @param encoding 请求编码
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public
static
Result get(String url, Map<String, String> headers, Map<String, String> params)
throws
ClientProtocolException, IOException {
cookieStore =
new
BasicCookieStore();
client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
url = (
null
== params ? url : url +
"?"
+ parseParam(params));
get =
new
HttpGet(url);
get.setHeaders(parseHeader(headers));
response = client.execute(get);
entity = response.getEntity();
result =
new
Result();
result.setHttpClient(client);
result.setCookies(cookieStore.getCookies());
result.setStatusCode(response.getStatusLine().getStatusCode());
result.setHeaders(response.getAllHeaders());
result.setHttpEntity(entity);
result.setBody(EntityUtils.toString(entity));
return
result;
}
/**
* //TODO post请求
*
* @param url 请求地址
* @param headers 请求头
* @param params 请求参数
* @param encoding 请求编码
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public
static
Result post(String url, Map<String, String> headers, Map<String, String> params, String encoding)
throws
ClientProtocolException, IOException {
cookieStore =
new
BasicCookieStore();
client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
post =
new
HttpPost(url);
List<NameValuePair> list =
new
ArrayList<NameValuePair>();
for
(String temp : params.keySet()) {
list.add(
new
BasicNameValuePair(temp, params.get(temp)));
}
post.setEntity(
new
UrlEncodedFormEntity(list, encoding));
post.setHeaders(parseHeader(headers));
response = client.execute(post);
entity = response.getEntity();
result =
new
Result();
result.setHttpClient(client);
result.setCookies(cookieStore.getCookies());
result.setStatusCode(response.getStatusLine().getStatusCode());
result.setHeaders(response.getAllHeaders());
result.setHttpEntity(entity);
result.setBody(EntityUtils.toString(entity));
return
result;
}
/**
* //TODO 转换header
*
* @param headers
* @return
*/
private
static
Header[] parseHeader(Map<String, String> headers) {
if
(
null
== headers || headers.isEmpty()) {
return
getDefaultHeaders();
}
Header[] allHeader =
new
BasicHeader[headers.size()];
int
i =
0
;
for
(String str : headers.keySet()) {
allHeader[i] =
new
BasicHeader(str, headers.get(str));
i++;
}
return
allHeader;
}
/**
* //TODO 默认header
*
* @return
*/
private
static
Header[] getDefaultHeaders() {
Header[] allHeader =
new
BasicHeader[
2
];
allHeader[
0
] =
new
BasicHeader(
"Content-Type"
,
"application/x-www-form-urlencoded"
);
allHeader[
1
] =
new
BasicHeader(
"User-Agent"
,
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36"
);
return
allHeader;
}
/**
* //TODO 转换参数列表
*
* @param params
* @return
*/
private
static
String parseParam(Map<String, String> params) {
if
(
null
== params || params.isEmpty()) {
return
""
;
}
StringBuffer sb =
new
StringBuffer();
for
(String key : params.keySet()) {
sb.append(key +
"="
+ params.get(key) +
"&"
);
}
return
sb.substring(
0
, sb.length() -
1
);
}
/**
* 释放httpclient对象
*/
public
static
void
closeClient(CloseableHttpClient client) {
if
(
null
!= client) {
try
{
client.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
}
|
1
2
3
4
5
6
7
8
9
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
67
68
69
70
71
|
/**
* 封装请求返回结果
* @author rex
*
*/
public
class
Result {
private
CloseableHttpClient httpClient;
private
List<Cookie> cookies;
private
HttpEntity httpEntity;
private
HashMap<String, Header> headerAll;
private
int
statusCode;
private
String body;
public
List<Cookie> getCookies() {
return
cookies;
}
public
void
setCookies(List<Cookie> cookies) {
this
.cookies = cookies;
}
public
CloseableHttpClient getHttpClient() {
return
httpClient;
}
public
void
setHttpClient(CloseableHttpClient httpClient) {
this
.httpClient = httpClient;
}
public
int
getStatusCode() {
return
statusCode;
}
public
void
setStatusCode(
int
statusCode) {
this
.statusCode = statusCode;
}
public
void
setHeaders(Header[] headers) {
headerAll =
new
HashMap<String, Header>();
for
(Header header : headers) {
headerAll.put(header.getName(), header);
}
}
public
HashMap<String, Header> getHeaderAll() {
return
headerAll;
}
public
void
setHttpEntity(HttpEntity entity) {
this
.httpEntity = entity;
}
public
HttpEntity getHttpEntity() {
return
httpEntity;
}
public
String getBody() {
return
body;
}
public
void
setBody(String body) {
this
.body = body;
}
}
|
一个登录小米的测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public
class
XiaoMiLogin {
public
static
void
main(String[] args) {
String login_url =
"https://account.xiaomi.com/pass/serviceLoginAuth2"
;
Map<String, String> params =
new
HashMap<String, String>();
params.put(
"user"
,
"小米账号"
);
params.put(
"pwd"
,
"密码"
);
params.put(
"callback"
,
"https://account.xiaomi.com"
);
params.put(
"sid"
,
"passport"
);
params.put(
"display"
,
"mobile"
);
params.put(
"qs"
,
"%3Fsid%3Dpassport"
);
params.put(
"_sign"
,
"KKkRvCpZoDC+gLdeyOsdMhwV0Xg="
);
try
{
Result r = HttpUtil.post(login_url,
null
, params,
"UTF-8"
);
for
(Cookie cookie : r.getCookies()){
System.out.println(cookie.getName() +
"="
+ cookie.getValue());
}
HttpUtil.closeClient(r.getHttpClient());
}
catch
(ClientProtocolException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
|