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