|  |  |  | 
|---|
|  |  |  | import java.security.SecureRandom; | 
|---|
|  |  |  | import java.security.cert.CertificateException; | 
|---|
|  |  |  | import java.security.cert.X509Certificate; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public class HttpsUtil { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static String get(String url,boolean ignoreSSL) { | 
|---|
|  |  |  | return connection(url, "GET", null, null,ignoreSSL); | 
|---|
|  |  |  | return connection(url, "GET", null, null,ignoreSSL,null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static String post(String url, String data, String contentType, boolean ignoreSSL) { | 
|---|
|  |  |  | return connection(url, "POST", data, contentType, ignoreSSL); | 
|---|
|  |  |  | return connection(url, "POST", data, contentType, ignoreSSL,null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public static String sendGet(String url) { | 
|---|
|  |  |  | if(url.startsWith("https://")){ | 
|---|
|  |  |  | return connection(url, "GET", "", "text/plain; charset=utf-8", true,null); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | return connectionHttp(url, "GET", "", "text/plain; charset=utf-8",null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public static String postJson(String url, String data) { | 
|---|
|  |  |  | if(url.startsWith("https://")){ | 
|---|
|  |  |  | return connection(url, "POST", data, "application/json", true); | 
|---|
|  |  |  | return connection(url, "POST", data, "application/json", true,null); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | return connectionHttp(url, "POST", data, "application/json"); | 
|---|
|  |  |  | return connectionHttp(url, "POST", data, "application/json",null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public static String postJsonWithHeaders(String url, String data, Map<String,String> headers) { | 
|---|
|  |  |  | if(url.startsWith("https://")){ | 
|---|
|  |  |  | return connection(url, "POST", data, "application/json", true,headers); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | return connectionHttp(url, "POST",  data, "application/json",headers); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static String connection(String url,String method,String data,String contentType,boolean ignoreSSL){ | 
|---|
|  |  |  | public static String connection(String url,String method,String data,String contentType,boolean ignoreSSL, Map<String,String> headers){ | 
|---|
|  |  |  | HttpsURLConnection connection = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | URL _url = new URL(url); | 
|---|
|  |  |  | 
|---|
|  |  |  | if(contentType != null){ | 
|---|
|  |  |  | connection.setRequestProperty("Content-Type", contentType); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (headers!=null && headers.size()>0) { | 
|---|
|  |  |  | for (String s : headers.keySet()) { | 
|---|
|  |  |  | connection.setRequestProperty(s, headers.get(s)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(ignoreSSL){ | 
|---|
|  |  |  | //信任所有ssl证书和主机 | 
|---|
|  |  |  | TrustManager[] trustManagers = {new HttpsTrustManager()}; | 
|---|
|  |  |  | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | connection.connect(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(data != null){ | 
|---|
|  |  |  | 
|---|
|  |  |  | outputStream.write(data.getBytes("utf-8")); | 
|---|
|  |  |  | outputStream.close(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int responseCode = connection.getResponseCode(); | 
|---|
|  |  |  | if (responseCode == HttpsURLConnection.HTTP_OK) { | 
|---|
|  |  |  | InputStream is = connection.getInputStream(); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public static String connectionHttp(String url,String method,String data,String contentType ){ | 
|---|
|  |  |  | public static String connectionHttp(String url,String method,String data,String contentType, Map<String,String> headers){ | 
|---|
|  |  |  | HttpURLConnection connection = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | URL _url = new URL(url); | 
|---|
|  |  |  | 
|---|
|  |  |  | if(contentType != null){ | 
|---|
|  |  |  | connection.setRequestProperty("Content-Type", contentType); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (headers!=null && headers.size()>0) { | 
|---|
|  |  |  | for (String s : headers.keySet()) { | 
|---|
|  |  |  | connection.setRequestProperty(s, headers.get(s)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | connection.connect(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(data != null){ | 
|---|