forked from CentMeng/JavaFrameTest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpProxy.java
More file actions
230 lines (210 loc) · 7.64 KB
/
Copy pathHttpProxy.java
File metadata and controls
230 lines (210 loc) · 7.64 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
package com.msj.network.utils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpProxy {
private static class SingletonHolder {
static final HttpProxy instance = new HttpProxy();
}
public static HttpProxy getInstance() {
return SingletonHolder.instance;
}
private static CloseableHttpClient httpClient;
private static final String CONTENT_TYPE_JSON = "application/json";
static {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(HttpProxyConfig.MAX_TOTAL_CONNECTIONS);
cm.setDefaultMaxPerRoute(HttpProxyConfig.MAX_ROUTE_CONNECTIONS);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(HttpProxyConfig.CONNECT_TIMEOUT)
.setConnectTimeout(HttpProxyConfig.CONNECT_TIMEOUT)
.build();
//缓存
/*
CacheConfig cacheConfig = CacheConfig.custom()
.setMaxCacheEntries(1000)
.setMaxObjectSize(8192)
.build();
*/
httpClient = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(cm)
.build();
}
public static HttpClient getHttpClient() {
return httpClient;
}
/**
* <B>方法名称:</B><BR>
* <B>概要说明:</B><BR>
* @param requestUrl 请求路径
* @return 字节数组
* @throws Exception
*/
public static byte[] get4Stream(String requestUrl) throws Exception {
byte[] ret = null;
HttpGet httpGet = new HttpGet(requestUrl);
CloseableHttpResponse response = httpClient.execute(httpGet);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
ret = EntityUtils.toByteArray(entity);
EntityUtils.consume(entity);
}
return ret;
} finally {
response.close();
}
}
/**
* <B>方法名称:</B><BR>
* <B>概要说明:</B><BR>
* @param requestUrl 请求路径
* @return 字符串
* @throws Exception
*/
public static String get4String(String requestUrl) throws Exception {
String ret = null;
HttpGet httpGet = new HttpGet(requestUrl);
CloseableHttpResponse response = httpClient.execute(httpGet);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
ret = EntityUtils.toString(entity);
EntityUtils.consume(entity);
}
return ret;
} finally {
response.close();
}
}
/**
* <B>方法名称:</B>普通请求<BR>
* <B>概要说明:</B>普通请求<BR>
* @param requestUrl 请求路径
* @param requestContent 请求内容
* @return 返回响应结果
* @throws IOException
*/
public static String post(String requestUrl, String requestContent) throws IOException {
StringEntity requestEntity = new StringEntity(requestContent, Consts.UTF_8);
return execute(requestUrl,requestEntity);
}
/**
* <B>方法名称:</B>json请求<BR>
* <B>概要说明:</B>json请求<BR>
* @param requestUrl 请求路径
* @param jsonContent json内容
* @return 返回响应结果
* @throws IOException
*/
public static String postJson(String requestUrl, String jsonContent) throws IOException {
StringEntity requestEntity = new StringEntity(jsonContent, Consts.UTF_8);
requestEntity.setContentEncoding("UTF-8");
requestEntity.setContentType(CONTENT_TYPE_JSON);
return execute(requestUrl,requestEntity);
}
/**
* <B>方法名称:</B>模拟表单上传<BR>
* <B>概要说明:</B>模拟表单上传<BR>
* @param requestUrl 请求路径
* @param params 属性参数
* @return 返回响应结果
* @throws IOException
*/
public static String post(String requestUrl, Map<String, String> params) throws IOException {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if (params != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
EntityBuilder builder = EntityBuilder.create();
builder.setParameters(nvps);
HttpEntity httpEntity = builder.build();
return execute(requestUrl,httpEntity);
}
/**
* <B>方法名称:</B>上传文件<BR>
* <B>概要说明:</B>上传文件<BR>
* @param requestUrl 请求路径
* @param localFile 文件位置
* @param username 用户名
* @param password 密码
* @return 响应信息
* @throws IOException
*/
public static String upload(String requestUrl, String localFile, String username, String password) throws IOException {
HttpPost httpPost = new HttpPost(requestUrl);
// 把文件转换成流对象FileBody
FileBody fileBody = new FileBody(new File(localFile));
StringBody usernameInp = new StringBody(username, ContentType.create("text/plain", Consts.UTF_8));
StringBody passwordInp = new StringBody(password, ContentType.create("text/plain", Consts.UTF_8));
HttpEntity httpEntity = MultipartEntityBuilder.create()
// 相当于<input type="file" name="file"/>
.addPart("file", fileBody)
// 相当于<input type="text" name="userName" value=userName>
.addPart("username", usernameInp)
.addPart("password", passwordInp)
.build();
return execute(requestUrl,httpEntity);
}
/**
* <B>方法名称:</B>执行请求方法<BR>
* <B>概要说明:</B>执行请求方法<BR>
* @param requestUrl 请求路径
* @param httpEntity 请求实体对象
* @return 返回响应结果
* @throws IOException
*/
private static String execute(String requestUrl, HttpEntity httpEntity) throws IOException {
String result = null;
HttpPost httpPost = new HttpPost(requestUrl);
httpPost.setEntity(httpEntity);
try {
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
try {
HttpEntity entity = httpResponse.getEntity();
//System.out.println(httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getReasonPhrase().equals("OK") && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(entity, "UTF-8");
}
//进行销毁
EntityUtils.consume(entity);
} finally {
if (null != httpResponse) {
httpResponse.close();
}
}
} finally {
if (null != httpPost) {
httpPost.releaseConnection();
}
}
return result;
}
}