提交 | 用户 | 时间
|
58d006
|
1 |
package com.mandi.common; |
A |
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.IOException; |
|
5 |
import java.nio.charset.Charset; |
|
6 |
import java.util.ArrayList; |
|
7 |
import java.util.List; |
|
8 |
import java.util.Map; |
|
9 |
import java.util.Map.Entry; |
|
10 |
|
|
11 |
import javax.net.ssl.SSLContext; |
|
12 |
|
|
13 |
import org.apache.http.HttpEntity; |
|
14 |
import org.apache.http.NameValuePair; |
cc0cbf
|
15 |
import org.apache.http.client.config.RequestConfig; |
58d006
|
16 |
import org.apache.http.client.entity.UrlEncodedFormEntity; |
A |
17 |
import org.apache.http.client.methods.CloseableHttpResponse; |
|
18 |
import org.apache.http.client.methods.HttpGet; |
|
19 |
import org.apache.http.client.methods.HttpPost; |
|
20 |
import org.apache.http.conn.ssl.NoopHostnameVerifier; |
|
21 |
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
|
22 |
import org.apache.http.entity.StringEntity; |
|
23 |
import org.apache.http.entity.mime.MultipartEntityBuilder; |
|
24 |
import org.apache.http.impl.client.CloseableHttpClient; |
|
25 |
import org.apache.http.impl.client.HttpClients; |
|
26 |
import org.apache.http.message.BasicNameValuePair; |
|
27 |
import org.apache.http.ssl.SSLContexts; |
|
28 |
import org.apache.http.util.EntityUtils; |
|
29 |
|
|
30 |
import com.google.gson.JsonObject; |
|
31 |
import com.google.gson.JsonParser; |
|
32 |
|
|
33 |
/** |
|
34 |
* @author mengly |
|
35 |
* @version 创建时间:2015年10月27日 上午9:59:53 |
|
36 |
* 类说明 |
|
37 |
*/ |
|
38 |
|
|
39 |
public class Wxhtmethod { |
|
40 |
/** |
|
41 |
* new ssl(); |
|
42 |
* @return |
|
43 |
* @author mengly |
|
44 |
* @version 创建时间:2015年10月13日 上午11:21:15 |
|
45 |
*/ |
|
46 |
public static CloseableHttpClient nssl() { |
|
47 |
CloseableHttpClient httpclient = null; |
|
48 |
try { |
|
49 |
SSLContext sslcontext = SSLContexts.createDefault(); |
|
50 |
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE); |
|
51 |
httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); |
|
52 |
}catch(Exception ee){ |
|
53 |
ee.printStackTrace(); |
|
54 |
} |
|
55 |
return httpclient; |
|
56 |
} |
|
57 |
public static String get(String url) |
|
58 |
{ |
|
59 |
CloseableHttpClient httpclient = HttpClients.createDefault(); |
|
60 |
HttpGet hp=new HttpGet(url); |
|
61 |
String re=null; |
|
62 |
try { |
|
63 |
CloseableHttpResponse cr=null; |
|
64 |
cr=httpclient.execute(hp); |
|
65 |
try{ |
|
66 |
HttpEntity he=cr.getEntity(); |
|
67 |
if(he!=null) |
|
68 |
re=EntityUtils.toString(he,"UTF-8"); |
|
69 |
}catch(Exception ee) |
|
70 |
{}finally{ |
|
71 |
cr.close(); |
|
72 |
} |
|
73 |
} catch (Exception e) { |
|
74 |
e.printStackTrace(); |
|
75 |
}finally{ |
|
76 |
try { |
|
77 |
httpclient.close(); |
|
78 |
} catch (IOException e) { |
|
79 |
e.printStackTrace(); |
|
80 |
} |
|
81 |
} |
|
82 |
return re; |
|
83 |
} |
|
84 |
public static String ssl_get(String url) |
|
85 |
{ |
|
86 |
CloseableHttpClient httpclient =nssl(); |
|
87 |
HttpGet hp=new HttpGet(url); |
|
88 |
String re=null; |
|
89 |
try { |
|
90 |
CloseableHttpResponse cr=null; |
|
91 |
cr=httpclient.execute(hp); |
|
92 |
try{ |
|
93 |
HttpEntity he=cr.getEntity(); |
|
94 |
if(he!=null) |
|
95 |
re=EntityUtils.toString(he,"UTF-8"); |
|
96 |
}catch(Exception ee) |
|
97 |
{}finally{ |
|
98 |
cr.close(); |
|
99 |
} |
|
100 |
} catch (Exception e) { |
|
101 |
e.printStackTrace(); |
|
102 |
}finally{ |
|
103 |
try { |
|
104 |
httpclient.close(); |
|
105 |
} catch (IOException e) { |
|
106 |
e.printStackTrace(); |
|
107 |
} |
|
108 |
} |
|
109 |
return re; |
|
110 |
} |
|
111 |
public static JsonObject postJson(String url,Map<String, String> params) |
|
112 |
{ |
|
113 |
JsonObject jo=new JsonObject(); |
|
114 |
CloseableHttpClient httpclient = HttpClients.createDefault(); |
|
115 |
HttpPost hp=new HttpPost(url); |
4c64dc
|
116 |
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(30000).build(); |
cc0cbf
|
117 |
hp.setConfig(config); |
58d006
|
118 |
String re=null; |
A |
119 |
List<NameValuePair> fps=new ArrayList<NameValuePair>(); |
|
120 |
UrlEncodedFormEntity entity=null; |
|
121 |
if(params!=null) |
|
122 |
{ |
|
123 |
for (Entry<String, String> en : params.entrySet()) { |
|
124 |
fps.add(new BasicNameValuePair(en.getKey(), en.getValue())); |
|
125 |
} |
|
126 |
} |
|
127 |
try { |
|
128 |
entity=new UrlEncodedFormEntity(fps,"UTF-8"); |
|
129 |
hp.setEntity(entity); |
|
130 |
CloseableHttpResponse cr=null; |
|
131 |
cr= httpclient.execute(hp); |
|
132 |
try{ |
|
133 |
int status=cr.getStatusLine().getStatusCode(); |
|
134 |
if(status!=200){ |
|
135 |
jo.addProperty("code", 1); |
|
136 |
jo.addProperty("errmsg", "网络不通!,网络状态码为:"+status); |
|
137 |
return jo; |
|
138 |
} |
|
139 |
HttpEntity he=cr.getEntity(); |
|
140 |
if(he!=null){ |
|
141 |
re=EntityUtils.toString(he,"UTF-8"); |
|
142 |
try{ |
|
143 |
JsonParser jp=new JsonParser(); |
|
144 |
JsonObject jo1=jp.parse(re).getAsJsonObject(); |
|
145 |
return jo1; |
|
146 |
}catch(Exception e12){ |
|
147 |
jo.addProperty("code", 1); |
|
148 |
jo.addProperty("errmsg", e12.getMessage()); |
|
149 |
return jo; |
|
150 |
} |
|
151 |
} |
|
152 |
}catch(Exception ee){ |
|
153 |
jo.addProperty("code", 1); |
|
154 |
jo.addProperty("errmsg", ee.getMessage()); |
|
155 |
return jo; |
|
156 |
}finally{ |
|
157 |
cr.close(); |
|
158 |
} |
|
159 |
} catch (Exception e) { |
|
160 |
jo.addProperty("code", 1); |
|
161 |
jo.addProperty("errmsg", e.getMessage()); |
|
162 |
return jo; |
|
163 |
}finally{ |
|
164 |
try { |
|
165 |
httpclient.close(); |
|
166 |
} catch (IOException e) { |
|
167 |
e.printStackTrace(); |
|
168 |
} |
|
169 |
} |
|
170 |
return jo; |
|
171 |
} |
|
172 |
public static JsonObject postJsonStr(String url,String jsonStr) |
|
173 |
{ |
|
174 |
JsonObject jo=new JsonObject(); |
|
175 |
CloseableHttpClient httpclient = HttpClients.createDefault(); |
|
176 |
String re=null; |
|
177 |
try { |
|
178 |
// StringEntity s = new StringEntity(jsonStr==null?"":jsonStr); |
|
179 |
// s.setContentEncoding("UTF-8"); |
|
180 |
// hp.setEntity(s); |
|
181 |
HttpPost hp=new HttpPost(url); |
4c64dc
|
182 |
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(30000).build(); |
cc0cbf
|
183 |
hp.setConfig(config); |
58d006
|
184 |
StringEntity se=new StringEntity(jsonStr, Charset.forName("utf-8")); |
A |
185 |
se.setContentType("application/json"); |
|
186 |
hp.setEntity(se); |
|
187 |
CloseableHttpResponse cr=null; |
|
188 |
cr= httpclient.execute(hp); |
|
189 |
try{ |
|
190 |
int status=cr.getStatusLine().getStatusCode(); |
|
191 |
if(status!=200){ |
|
192 |
jo.addProperty("code", 1); |
|
193 |
jo.addProperty("errmsg", "网络不通!,网络状态码为:"+status); |
|
194 |
return jo; |
|
195 |
} |
|
196 |
HttpEntity he=cr.getEntity(); |
|
197 |
if(he!=null){ |
|
198 |
re=EntityUtils.toString(he,"UTF-8"); |
|
199 |
try{ |
|
200 |
JsonParser jp=new JsonParser(); |
|
201 |
JsonObject jo1=jp.parse(re).getAsJsonObject(); |
|
202 |
return jo1; |
|
203 |
}catch(Exception e12){ |
|
204 |
jo.addProperty("code", 1); |
|
205 |
jo.addProperty("errmsg", e12.getMessage()); |
|
206 |
return jo; |
|
207 |
} |
|
208 |
} |
|
209 |
}catch(Exception ee){ |
|
210 |
jo.addProperty("code", 1); |
|
211 |
jo.addProperty("errmsg", ee.getMessage()); |
|
212 |
return jo; |
|
213 |
}finally{ |
|
214 |
cr.close(); |
|
215 |
} |
|
216 |
} catch (Exception e) { |
|
217 |
jo.addProperty("code", 1); |
|
218 |
jo.addProperty("errmsg", e.getMessage()); |
|
219 |
return jo; |
|
220 |
}finally{ |
|
221 |
try { |
|
222 |
httpclient.close(); |
|
223 |
} catch (IOException e) { |
|
224 |
e.printStackTrace(); |
|
225 |
} |
|
226 |
} |
|
227 |
return jo; |
|
228 |
} |
|
229 |
|
|
230 |
|
|
231 |
public static JsonObject getJson(String url) |
|
232 |
{ |
|
233 |
JsonObject jo=new JsonObject(); |
|
234 |
CloseableHttpClient httpclient = HttpClients.createDefault(); |
|
235 |
HttpGet hp=new HttpGet(url); |
|
236 |
String re=null; |
|
237 |
try { |
|
238 |
CloseableHttpResponse cr=null; |
|
239 |
cr= httpclient.execute(hp); |
|
240 |
try{ |
|
241 |
int status=cr.getStatusLine().getStatusCode(); |
|
242 |
if(status!=200){ |
|
243 |
jo.addProperty("code", 1); |
|
244 |
jo.addProperty("errmsg", "网络不通!,网络状态码为:"+status); |
|
245 |
return jo; |
|
246 |
} |
|
247 |
HttpEntity he=cr.getEntity(); |
|
248 |
if(he!=null){ |
|
249 |
re=EntityUtils.toString(he,"UTF-8"); |
|
250 |
try{ |
|
251 |
JsonParser jp=new JsonParser(); |
|
252 |
JsonObject jo1=jp.parse(re).getAsJsonObject(); |
|
253 |
return jo1; |
|
254 |
}catch(Exception e12){ |
|
255 |
jo.addProperty("code", 1); |
|
256 |
jo.addProperty("errmsg", e12.getMessage()); |
|
257 |
return jo; |
|
258 |
} |
|
259 |
} |
|
260 |
}catch(Exception ee){ |
|
261 |
jo.addProperty("code", 1); |
|
262 |
jo.addProperty("errmsg", ee.getMessage()); |
|
263 |
return jo; |
|
264 |
}finally{ |
|
265 |
cr.close(); |
|
266 |
} |
|
267 |
} catch (Exception e) { |
|
268 |
jo.addProperty("code", 1); |
|
269 |
jo.addProperty("errmsg", e.getMessage()); |
|
270 |
return jo; |
|
271 |
}finally{ |
|
272 |
try { |
|
273 |
httpclient.close(); |
|
274 |
} catch (IOException e) { |
|
275 |
e.printStackTrace(); |
|
276 |
} |
|
277 |
} |
|
278 |
return jo; |
|
279 |
} |
|
280 |
public static String post(String url,Map<String, String> params) |
|
281 |
{ |
|
282 |
CloseableHttpClient httpclient = HttpClients.createDefault(); |
|
283 |
HttpPost hp=new HttpPost(url); |
4c64dc
|
284 |
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(30000).build(); |
cc0cbf
|
285 |
hp.setConfig(config); |
58d006
|
286 |
String re=null; |
A |
287 |
List<NameValuePair> fps=new ArrayList<NameValuePair>(); |
|
288 |
UrlEncodedFormEntity entity=null; |
|
289 |
if(params!=null) |
|
290 |
{ |
|
291 |
for (Entry<String, String> en : params.entrySet()) { |
|
292 |
fps.add(new BasicNameValuePair(en.getKey(), en.getValue())); |
|
293 |
} |
|
294 |
} |
|
295 |
try { |
|
296 |
entity=new UrlEncodedFormEntity(fps,"UTF-8"); |
|
297 |
hp.setEntity(entity); |
|
298 |
CloseableHttpResponse cr=null; |
|
299 |
cr= httpclient.execute(hp); |
|
300 |
try{ |
|
301 |
HttpEntity he=cr.getEntity(); |
|
302 |
if(he!=null) |
|
303 |
re=EntityUtils.toString(he,"UTF-8"); |
|
304 |
}catch(Exception ee) |
|
305 |
{}finally{ |
|
306 |
cr.close(); |
|
307 |
} |
|
308 |
} catch (Exception e) { |
|
309 |
}finally{ |
|
310 |
try { |
|
311 |
httpclient.close(); |
|
312 |
} catch (IOException e) { |
|
313 |
e.printStackTrace(); |
|
314 |
} |
|
315 |
} |
|
316 |
return re; |
|
317 |
} |
|
318 |
|
|
319 |
public static String ssl_post(String url,Map<String, String> params) |
|
320 |
{ |
|
321 |
CloseableHttpClient httpclient=nssl(); |
|
322 |
// CloseableHttpClient httpclient = HttpClients.createDefault(); |
|
323 |
HttpPost hp=new HttpPost(url); |
4c64dc
|
324 |
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(30000).build(); |
cc0cbf
|
325 |
hp.setConfig(config); |
58d006
|
326 |
String re=null; |
A |
327 |
List<NameValuePair> fps=new ArrayList<NameValuePair>(); |
|
328 |
UrlEncodedFormEntity entity=null; |
|
329 |
if(params!=null) |
|
330 |
{ |
|
331 |
for (Entry<String, String> en : params.entrySet()) { |
|
332 |
fps.add(new BasicNameValuePair(en.getKey(), en.getValue())); |
|
333 |
} |
|
334 |
} |
|
335 |
try { |
|
336 |
entity=new UrlEncodedFormEntity(fps,"UTF-8"); |
|
337 |
hp.setEntity(entity); |
|
338 |
CloseableHttpResponse cr=null; |
|
339 |
cr=httpclient.execute(hp); |
|
340 |
try{ |
|
341 |
HttpEntity he=cr.getEntity(); |
|
342 |
if(he!=null) |
|
343 |
re=EntityUtils.toString(he,"UTF-8"); |
|
344 |
}catch(Exception ee) |
|
345 |
{}finally{ |
|
346 |
cr.close(); |
|
347 |
} |
|
348 |
} catch (Exception e) { |
|
349 |
e.printStackTrace(); |
|
350 |
}finally{ |
|
351 |
try { |
|
352 |
httpclient.close(); |
|
353 |
} catch (IOException e) { |
|
354 |
e.printStackTrace(); |
|
355 |
} |
|
356 |
} |
|
357 |
return re; |
|
358 |
} |
|
359 |
|
|
360 |
public static String ssl_postmultipart(String url,Map<String, String> params,String upfilename,File file) |
|
361 |
{ |
|
362 |
CloseableHttpClient httpclient=nssl(); |
|
363 |
HttpPost hp=new HttpPost(url); |
4c64dc
|
364 |
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(30000).build(); |
cc0cbf
|
365 |
hp.setConfig(config); |
58d006
|
366 |
MultipartEntityBuilder me=MultipartEntityBuilder.create(); |
A |
367 |
me.addBinaryBody(upfilename, file); |
|
368 |
if(params!=null) |
|
369 |
for (Entry<String, String> en : params.entrySet()) { |
|
370 |
me.addTextBody(en.getKey(), en.getValue()); |
|
371 |
} |
|
372 |
hp.setEntity(me.build()); |
|
373 |
String re=null; |
|
374 |
try { |
|
375 |
CloseableHttpResponse cr=null; |
|
376 |
cr=httpclient.execute(hp); |
|
377 |
try{ |
|
378 |
HttpEntity he=cr.getEntity(); |
|
379 |
if(he!=null) |
|
380 |
re=EntityUtils.toString(he,"UTF-8"); |
|
381 |
}catch(Exception ee) |
|
382 |
{}finally{ |
|
383 |
cr.close(); |
|
384 |
} |
|
385 |
} catch (Exception e) { |
|
386 |
e.printStackTrace(); |
|
387 |
}finally{ |
|
388 |
try { |
|
389 |
httpclient.close(); |
|
390 |
} catch (IOException e) { |
|
391 |
e.printStackTrace(); |
|
392 |
} |
|
393 |
} |
|
394 |
return re; |
|
395 |
} |
|
396 |
public static String ssl_postbody(String url,String body) |
|
397 |
{ |
|
398 |
CloseableHttpClient httpclient=nssl(); |
|
399 |
HttpPost hp=new HttpPost(url); |
4c64dc
|
400 |
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(30000).build(); |
cc0cbf
|
401 |
hp.setConfig(config); |
58d006
|
402 |
StringEntity se=new StringEntity(body, Charset.forName("utf-8")); |
A |
403 |
hp.setEntity(se); |
|
404 |
String re=null; |
|
405 |
try { |
|
406 |
CloseableHttpResponse cr=null; |
|
407 |
cr=httpclient.execute(hp); |
|
408 |
try{ |
|
409 |
HttpEntity he=cr.getEntity(); |
|
410 |
if(he!=null) |
|
411 |
re=EntityUtils.toString(he,"UTF-8"); |
|
412 |
}catch(Exception ee) |
|
413 |
{}finally{ |
|
414 |
cr.close(); |
|
415 |
} |
|
416 |
} catch (Exception e) { |
|
417 |
e.printStackTrace(); |
|
418 |
}finally{ |
|
419 |
try { |
|
420 |
httpclient.close(); |
|
421 |
} catch (IOException e) { |
|
422 |
e.printStackTrace(); |
|
423 |
} |
|
424 |
} |
|
425 |
return re; |
|
426 |
} |
|
427 |
public static String first2Upper(String str) |
|
428 |
{ |
|
429 |
if(str==null) |
|
430 |
return null; |
|
431 |
char[] cs=str.toCharArray(); |
|
432 |
cs[0]-=32; |
|
433 |
return String.valueOf(cs); |
|
434 |
} |
|
435 |
|
|
436 |
} |