forked from wswenyue/AndroidUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApacheHttpUtil.java
More file actions
300 lines (280 loc) · 8.99 KB
/
Copy pathApacheHttpUtil.java
File metadata and controls
300 lines (280 loc) · 8.99 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package org.iti.eyescare.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
/**
* 基于Apache HttpClient的封装,支持HTTP GET、POST请求,支持多文件上传
*
*
*/
public class ApacheHttpUtil {
// 设置URLConnection的连接超时
private final static int CONNET_TIMEOUT = 30 * 1000;
// 设置URLConnection的读取超时
private final static int READ_TIMEOUT = 30 * 1000;
/**
* HTTP GET请求
*
* @param url
* 请求链接
* @return HTTP GET请求结果
*/
public static String get(String url) {
return get(url, null);
}
/**
* HTTP GET请求
*
* @param url
* 请求链接
* @param params
* HTTP GET请求的QueryString封装map集合
* @return HTTP GET请求结果
*/
public static String get(String url, Map<String, String> params) {
try {
String realUrl = generateUrl(url, params);
HttpClient client = getNewHttpClient();
HttpGet getMethod = new HttpGet(realUrl);
HttpResponse response = client.execute(getMethod);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
for (String s = reader.readLine(); s != null; s = reader
.readLine()) {
builder.append(s);
}
String result = builder.toString();
return result;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* HTTP POST请求
*
* @param url
* 请求链接
* @param params
* HTTP POST请求body的封装map集合
* @return
*
*/
public static String post(String url, Map<String, String> params) {
try {
HttpClient client = getHttpClient();
HttpPost postMethod = new HttpPost(url);
List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
if (params != null && params.size() > 0) {
Iterator<Entry<String, String>> iterator = params.entrySet()
.iterator();
while (iterator.hasNext()) {
Entry<String, String> param = iterator.next();
String key = param.getKey();
String value = param.getValue();
BasicNameValuePair pair = new BasicNameValuePair(key, value);
pairs.add(pair);
}
postMethod
.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
}
HttpResponse response = client.execute(postMethod);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* HTTP POST请求上传文件,支持多文件上传
*
* @param url
* 请求链接
* @param params
* HTTP POST请求文本参数map集合
* @param files
* HTTP POST请求文件参数map集合
* @return HTTP POST请求结果
* @throws IOException
*/
public static String post(String url, Map<String, String> params,
Map<String, String> files) throws IOException {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder
.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
multipartEntityBuilder.addTextBody(entry.getKey(),
entry.getValue());
}
}
if (files != null && !files.isEmpty()) {
for (Map.Entry<String, String> entry : files.entrySet()) {
File file = new File(entry.getValue());
multipartEntityBuilder.addBinaryBody(entry.getKey(), file);
}
}
HttpClient client = getNewHttpClient();
HttpPost post = new HttpPost(url);
HttpEntity httpEntity = multipartEntityBuilder.build();
post.setEntity(httpEntity);
HttpResponse response = client.execute(post);
StringBuffer sb = new StringBuffer();
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity result = response.getEntity();
if (result != null) {
InputStream is = result.getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String tempLine;
while ((tempLine = br.readLine()) != null) {
sb.append(tempLine);
}
}
}
post.abort();
return sb.toString();
}
/**
* 上传图片
*
* @param url
* @param file
* @return
* @throws IOException
*/
public static String post(String url, File file) throws IOException {
HttpClient client = getNewHttpClient();
HttpPost post = new HttpPost(url);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder
.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
multipartEntityBuilder.addTextBody("fileName", file.getName());
multipartEntityBuilder.addPart("uploadFile", new FileBody(file));
HttpEntity httpEntity = multipartEntityBuilder.build();
post.setEntity(httpEntity);
HttpResponse response = client.execute(post);
StringBuffer sb = new StringBuffer();
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity result = response.getEntity();
if (result != null) {
InputStream is = result.getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String tempLine;
while ((tempLine = br.readLine()) != null) {
sb.append(tempLine);
}
}
}
post.abort();
return sb.toString();
}
/**
* 获取HttpClient
*
* @return
*/
public static HttpClient getHttpClient() {
BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, CONNET_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, READ_TIMEOUT);
HttpClient httpClient = new DefaultHttpClient(httpParams);
return httpClient;
}
/**
* 获取HttpClient
*
* @return
*/
private static HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore
.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new SSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
HttpConnectionParams.setConnectionTimeout(params, CONNET_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, READ_TIMEOUT);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(
params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
/**
* 根据基础url和参数拼接请求地址
*
* @param url
* @param params
* @return
*/
private static String generateUrl(String url, Map<String, String> params) {
StringBuilder urlBuilder = new StringBuilder(url);
if (null != params) {
urlBuilder.append("?");
Iterator<Entry<String, String>> iterator = params.entrySet()
.iterator();
while (iterator.hasNext()) {
Entry<String, String> param = iterator.next();
String key = param.getKey();
String value = param.getValue();
urlBuilder.append(key).append('=').append(value);
if (iterator.hasNext()) {
urlBuilder.append('&');
}
}
}
return urlBuilder.toString();
}
}