package com.mandi.common; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.net.ssl.SSLContext; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; 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.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.ssl.SSLContexts; import org.apache.http.util.EntityUtils; import com.google.gson.JsonObject; import com.google.gson.JsonParser; /** * @author mengly * @version 创建时间:2015年10月27日 上午9:59:53 * 类说明 */ public class Wxhtmethod { /** * new ssl(); * @return * @author mengly * @version 创建时间:2015年10月13日 上午11:21:15 */ public static CloseableHttpClient nssl() { CloseableHttpClient httpclient = null; try { SSLContext sslcontext = SSLContexts.createDefault(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE); httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); }catch(Exception ee){ ee.printStackTrace(); } return httpclient; } public static String get(String url) { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet hp=new HttpGet(url); String re=null; try { CloseableHttpResponse cr=null; cr=httpclient.execute(hp); try{ HttpEntity he=cr.getEntity(); if(he!=null) re=EntityUtils.toString(he,"UTF-8"); }catch(Exception ee) {}finally{ cr.close(); } } catch (Exception e) { e.printStackTrace(); }finally{ try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return re; } public static String ssl_get(String url) { CloseableHttpClient httpclient =nssl(); HttpGet hp=new HttpGet(url); String re=null; try { CloseableHttpResponse cr=null; cr=httpclient.execute(hp); try{ HttpEntity he=cr.getEntity(); if(he!=null) re=EntityUtils.toString(he,"UTF-8"); }catch(Exception ee) {}finally{ cr.close(); } } catch (Exception e) { e.printStackTrace(); }finally{ try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return re; } public static JsonObject postJson(String url,Map params) { JsonObject jo=new JsonObject(); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost hp=new HttpPost(url); String re=null; List fps=new ArrayList(); UrlEncodedFormEntity entity=null; if(params!=null) { for (Entry en : params.entrySet()) { fps.add(new BasicNameValuePair(en.getKey(), en.getValue())); } } try { entity=new UrlEncodedFormEntity(fps,"UTF-8"); hp.setEntity(entity); CloseableHttpResponse cr=null; cr= httpclient.execute(hp); try{ int status=cr.getStatusLine().getStatusCode(); if(status!=200){ jo.addProperty("code", 1); jo.addProperty("errmsg", "网络不通!,网络状态码为:"+status); return jo; } HttpEntity he=cr.getEntity(); if(he!=null){ re=EntityUtils.toString(he,"UTF-8"); try{ JsonParser jp=new JsonParser(); JsonObject jo1=jp.parse(re).getAsJsonObject(); return jo1; }catch(Exception e12){ jo.addProperty("code", 1); jo.addProperty("errmsg", e12.getMessage()); return jo; } } }catch(Exception ee){ jo.addProperty("code", 1); jo.addProperty("errmsg", ee.getMessage()); return jo; }finally{ cr.close(); } } catch (Exception e) { jo.addProperty("code", 1); jo.addProperty("errmsg", e.getMessage()); return jo; }finally{ try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return jo; } public static JsonObject postJsonStr(String url,String jsonStr) { JsonObject jo=new JsonObject(); CloseableHttpClient httpclient = HttpClients.createDefault(); String re=null; try { // StringEntity s = new StringEntity(jsonStr==null?"":jsonStr); // s.setContentEncoding("UTF-8"); // hp.setEntity(s); HttpPost hp=new HttpPost(url); StringEntity se=new StringEntity(jsonStr, Charset.forName("utf-8")); se.setContentType("application/json"); hp.setEntity(se); CloseableHttpResponse cr=null; cr= httpclient.execute(hp); try{ int status=cr.getStatusLine().getStatusCode(); if(status!=200){ jo.addProperty("code", 1); jo.addProperty("errmsg", "网络不通!,网络状态码为:"+status); return jo; } HttpEntity he=cr.getEntity(); if(he!=null){ re=EntityUtils.toString(he,"UTF-8"); try{ JsonParser jp=new JsonParser(); JsonObject jo1=jp.parse(re).getAsJsonObject(); return jo1; }catch(Exception e12){ jo.addProperty("code", 1); jo.addProperty("errmsg", e12.getMessage()); return jo; } } }catch(Exception ee){ jo.addProperty("code", 1); jo.addProperty("errmsg", ee.getMessage()); return jo; }finally{ cr.close(); } } catch (Exception e) { jo.addProperty("code", 1); jo.addProperty("errmsg", e.getMessage()); return jo; }finally{ try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return jo; } public static JsonObject getJson(String url) { JsonObject jo=new JsonObject(); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet hp=new HttpGet(url); String re=null; try { CloseableHttpResponse cr=null; cr= httpclient.execute(hp); try{ int status=cr.getStatusLine().getStatusCode(); if(status!=200){ jo.addProperty("code", 1); jo.addProperty("errmsg", "网络不通!,网络状态码为:"+status); return jo; } HttpEntity he=cr.getEntity(); if(he!=null){ re=EntityUtils.toString(he,"UTF-8"); try{ JsonParser jp=new JsonParser(); JsonObject jo1=jp.parse(re).getAsJsonObject(); return jo1; }catch(Exception e12){ jo.addProperty("code", 1); jo.addProperty("errmsg", e12.getMessage()); return jo; } } }catch(Exception ee){ jo.addProperty("code", 1); jo.addProperty("errmsg", ee.getMessage()); return jo; }finally{ cr.close(); } } catch (Exception e) { jo.addProperty("code", 1); jo.addProperty("errmsg", e.getMessage()); return jo; }finally{ try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return jo; } public static String post(String url,Map params) { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost hp=new HttpPost(url); String re=null; List fps=new ArrayList(); UrlEncodedFormEntity entity=null; if(params!=null) { for (Entry en : params.entrySet()) { fps.add(new BasicNameValuePair(en.getKey(), en.getValue())); } } try { entity=new UrlEncodedFormEntity(fps,"UTF-8"); hp.setEntity(entity); CloseableHttpResponse cr=null; cr= httpclient.execute(hp); try{ HttpEntity he=cr.getEntity(); if(he!=null) re=EntityUtils.toString(he,"UTF-8"); }catch(Exception ee) {}finally{ cr.close(); } } catch (Exception e) { }finally{ try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return re; } public static String ssl_post(String url,Map params) { CloseableHttpClient httpclient=nssl(); // CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost hp=new HttpPost(url); String re=null; List fps=new ArrayList(); UrlEncodedFormEntity entity=null; if(params!=null) { for (Entry en : params.entrySet()) { fps.add(new BasicNameValuePair(en.getKey(), en.getValue())); } } try { entity=new UrlEncodedFormEntity(fps,"UTF-8"); hp.setEntity(entity); CloseableHttpResponse cr=null; cr=httpclient.execute(hp); try{ HttpEntity he=cr.getEntity(); if(he!=null) re=EntityUtils.toString(he,"UTF-8"); }catch(Exception ee) {}finally{ cr.close(); } } catch (Exception e) { e.printStackTrace(); }finally{ try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return re; } public static String ssl_postmultipart(String url,Map params,String upfilename,File file) { CloseableHttpClient httpclient=nssl(); HttpPost hp=new HttpPost(url); MultipartEntityBuilder me=MultipartEntityBuilder.create(); me.addBinaryBody(upfilename, file); if(params!=null) for (Entry en : params.entrySet()) { me.addTextBody(en.getKey(), en.getValue()); } hp.setEntity(me.build()); String re=null; try { CloseableHttpResponse cr=null; cr=httpclient.execute(hp); try{ HttpEntity he=cr.getEntity(); if(he!=null) re=EntityUtils.toString(he,"UTF-8"); }catch(Exception ee) {}finally{ cr.close(); } } catch (Exception e) { e.printStackTrace(); }finally{ try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return re; } public static String ssl_postbody(String url,String body) { CloseableHttpClient httpclient=nssl(); HttpPost hp=new HttpPost(url); StringEntity se=new StringEntity(body, Charset.forName("utf-8")); hp.setEntity(se); String re=null; try { CloseableHttpResponse cr=null; cr=httpclient.execute(hp); try{ HttpEntity he=cr.getEntity(); if(he!=null) re=EntityUtils.toString(he,"UTF-8"); }catch(Exception ee) {}finally{ cr.close(); } } catch (Exception e) { e.printStackTrace(); }finally{ try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return re; } public static String first2Upper(String str) { if(str==null) return null; char[] cs=str.toCharArray(); cs[0]-=32; return String.valueOf(cs); } }