| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- /**
- * @Filename:HttpsUtil.java
- * @Author:caiqf
- * @Date�?013-9-23
- */
- package com.happy.Unitil_nsh;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import javax.net.ssl.*;
- import java.io.*;
- import java.net.URL;
- import java.security.cert.CertificateException;
- import java.security.cert.X509Certificate;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.TreeMap;
- import java.util.zip.GZIPInputStream;
- /**
- * @Class:HttpsUtil.java
- * @Description�?
- * @Author:caiqf
- * @Date�?013-9-23
- */
- public class HttpsUtil {
- private static final Log log = LogFactory.getLog(HttpsUtil.class);
- private static class MyTrustManager implements X509TrustManager {
- public void checkClientTrusted(X509Certificate[] chain, String authType)
- throws CertificateException {
- }
- public void checkServerTrusted(X509Certificate[] chain, String authType)
- throws CertificateException {
- }
- public X509Certificate[] getAcceptedIssuers() {
- return new X509Certificate[]{};
- }
- }
- private static class MyHostnameVerifier implements HostnameVerifier {
- public boolean verify(String hostname, SSLSession session) {
- return true;
- }
- }
- /**
- * HTTP协议GET请求方法
- */
- public static String httpMethodGet(String url, String gb) {
- if (null == gb || "".equals(gb)) {
- gb = "UTF-8";
- }
- StringBuffer sb = new StringBuffer();
- URL urls;
- HttpsURLConnection uc = null;
- BufferedReader in = null;
- try {
- SSLContext sc = SSLContext.getInstance("SSL");
- sc.init(null, new TrustManager[]{new MyTrustManager()}, new java.security.SecureRandom());
- urls = new URL(url);
- uc = (HttpsURLConnection) urls.openConnection();
- uc.setSSLSocketFactory(sc.getSocketFactory());
- uc.setHostnameVerifier(new MyHostnameVerifier());
- uc.setRequestMethod("GET");
- uc.connect();
- in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "utf-8"));
- String readLine = "";
- while ((readLine = in.readLine()) != null) {
- sb.append(readLine);
- }
- if (in != null) {
- in.close();
- }
- if (uc != null) {
- uc.disconnect();
- }
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- } finally {
- if (uc != null) {
- uc.disconnect();
- }
- }
- return sb.toString();
- }
- /**
- * HTTP协议GET请求方法
- */
- public static byte[] httpMethodGetFile(String url) {
- byte[] btFile = null;
- URL urls = null;
- HttpsURLConnection uc = null;
- try {
- SSLContext sc = SSLContext.getInstance("SSL");
- sc.init(null, new TrustManager[]{new MyTrustManager()},
- new java.security.SecureRandom());
- urls = new URL(url);
- uc = (HttpsURLConnection) urls.openConnection();
- uc.setSSLSocketFactory(sc.getSocketFactory());
- uc.setHostnameVerifier(new MyHostnameVerifier());
- uc.setRequestMethod("GET");
- uc.connect();
- InputStream inStream = uc.getInputStream(); // 获取文件流二进制数据
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int len = 0;
- while ((len = inStream.read(buffer)) != -1) {
- outStream.write(buffer, 0, len);
- }
- inStream.close();
- btFile = outStream.toByteArray();
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- } finally {
- if (uc != null) {
- uc.disconnect();
- }
- }
- return btFile;
- }
- /**
- * HTTP协议POST请求方法
- */
- public static String httpMethodPost(String url, String params, String gb) {
- if (null == gb || "".equals(gb)) {
- gb = "UTF-8";
- }
- StringBuffer sb = new StringBuffer();
- URL urls;
- HttpsURLConnection uc = null;
- BufferedReader in = null;
- try {
- SSLContext sc = SSLContext.getInstance("SSL");
- sc.init(null, new TrustManager[]{new MyTrustManager()}, new java.security.SecureRandom());
- urls = new URL(url);
- uc = (HttpsURLConnection) urls.openConnection();
- uc.setSSLSocketFactory(sc.getSocketFactory());
- uc.setHostnameVerifier(new MyHostnameVerifier());
- uc.setRequestMethod("POST");
- uc.setDoOutput(true);
- uc.setDoInput(true);
- uc.setUseCaches(false);
- uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- uc.connect();
- DataOutputStream out = new DataOutputStream(uc.getOutputStream());
- out.write(params.getBytes(gb));
- out.flush();
- out.close();
- in = new BufferedReader(new InputStreamReader(uc.getInputStream(), gb));
- String readLine = "";
- while ((readLine = in.readLine()) != null) {
- sb.append(readLine);
- }
- if (in != null) {
- in.close();
- }
- if (uc != null) {
- uc.disconnect();
- }
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- } finally {
- if (uc != null) {
- uc.disconnect();
- }
- }
- return sb.toString();
- }
- /**
- * HTTP协议POST请求方法
- */
- public static byte[] httpMethodPostFile(String url, String params, String gb) {
- if (null == gb || "".equals(gb)) {
- gb = "UTF-8";
- }
- byte[] btFile = null;
- URL urls = null;
- HttpsURLConnection uc = null;
- try {
- SSLContext sc = SSLContext.getInstance("SSL");
- sc.init(null, new TrustManager[]{new MyTrustManager()}, new java.security.SecureRandom());
- urls = new URL(url);
- uc = (HttpsURLConnection) urls.openConnection();
- uc.setSSLSocketFactory(sc.getSocketFactory());
- uc.setHostnameVerifier(new MyHostnameVerifier());
- uc.setRequestMethod("POST");
- uc.setDoOutput(true);
- uc.setDoInput(true);
- uc.setUseCaches(false);
- uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- uc.connect();
- DataOutputStream out = new DataOutputStream(uc.getOutputStream());
- out.write(params.getBytes(gb));
- out.flush();
- out.close();
- InputStream inStream = uc.getInputStream(); // 获取文件流二进制数据
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int len = 0;
- while ((len = inStream.read(buffer)) != -1) {
- outStream.write(buffer, 0, len);
- }
- inStream.close();
- btFile = outStream.toByteArray();
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- } finally {
- if (uc != null) {
- uc.disconnect();
- }
- }
- return btFile;
- }
- /**
- * HTTP协议POST请求方法
- */
- public static String httpMethodPost(String url, TreeMap<String, String> paramsMap, String gb) {
- if (null == gb || "".equals(gb)) {
- gb = "UTF-8";
- }
- String params = null;
- if (null != paramsMap) {
- params = getParamStr(paramsMap);
- }
- System.out.println("====post请求参数= " + params);
- StringBuffer sb = new StringBuffer();
- URL urls;
- HttpsURLConnection uc = null;
- BufferedReader in = null;
- try {
- SSLContext sc = SSLContext.getInstance("SSL");
- sc.init(null, new TrustManager[]{new MyTrustManager()}, new java.security.SecureRandom());
- urls = new URL(url);
- uc = (HttpsURLConnection) urls.openConnection();
- uc.setSSLSocketFactory(sc.getSocketFactory());
- uc.setHostnameVerifier(new MyHostnameVerifier());
- uc.setRequestMethod("POST");
- uc.setDoOutput(true);
- uc.setDoInput(true);
- uc.setUseCaches(false);
- uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- uc.connect();
- DataOutputStream out = new DataOutputStream(uc.getOutputStream());
- out.write(params.getBytes(gb));
- out.flush();
- out.close();
- in = new BufferedReader(new InputStreamReader(uc.getInputStream(), gb));
- String readLine = "";
- while ((readLine = in.readLine()) != null) {
- sb.append(readLine);
- }
- if (in != null) {
- in.close();
- }
- if (uc != null) {
- uc.disconnect();
- }
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- } finally {
- if (uc != null) {
- uc.disconnect();
- }
- }
- return sb.toString();
- }
- public static String httpMethodPostGZIP(String url, TreeMap<String, String> paramsMap, String gb) {
- if (null == gb || "".equals(gb)) {
- gb = "UTF-8";
- }
- String params = null;
- if (null != paramsMap) {
- params = getParamStr(paramsMap);
- }
- System.out.println("==================post请求地址:" + url);
- System.out.println("==================post请求参数:" + params);
- StringBuffer sb = new StringBuffer();
- URL urls;
- HttpsURLConnection uc = null;
- BufferedReader in = null;
- try {
- SSLContext sc = SSLContext.getInstance("SSL");
- sc.init(null, new TrustManager[]{new MyTrustManager()}, new java.security.SecureRandom());
- urls = new URL(url);
- uc = (HttpsURLConnection) urls.openConnection();
- uc.setSSLSocketFactory(sc.getSocketFactory());
- uc.setHostnameVerifier(new MyHostnameVerifier());
- uc.setRequestMethod("POST");
- uc.setDoOutput(true);
- uc.setDoInput(true);
- uc.setUseCaches(false);
- uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- uc.connect();
- DataOutputStream out = new DataOutputStream(uc.getOutputStream());
- out.write(params.getBytes(gb));
- out.flush();
- out.close();
- in = new BufferedReader(new InputStreamReader(new GZIPInputStream(uc.getInputStream()), gb));
- String readLine = "";
- while ((readLine = in.readLine()) != null) {
- sb.append(readLine).append("\n");
- }
- if (in != null) {
- in.close();
- }
- if (uc != null) {
- uc.disconnect();
- }
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- } finally {
- if (uc != null) {
- uc.disconnect();
- }
- }
- return sb.toString();
- }
- /**
- * HTTP协议POST请求方法
- */
- public static byte[] httpMethodPostFile(String url, TreeMap<String, String> paramsMap, String gb) {
- if (null == gb || "".equals(gb)) {
- gb = "UTF-8";
- }
- String params = null;
- if (null != paramsMap) {
- params = getParamStr(paramsMap);
- }
- byte[] btFile = null;
- URL urls = null;
- HttpsURLConnection uc = null;
- try {
- SSLContext sc = SSLContext.getInstance("SSL");
- sc.init(null, new TrustManager[]{new MyTrustManager()}, new java.security.SecureRandom());
- urls = new URL(url);
- uc = (HttpsURLConnection) urls.openConnection();
- uc.setSSLSocketFactory(sc.getSocketFactory());
- uc.setHostnameVerifier(new MyHostnameVerifier());
- uc.setRequestMethod("POST");
- uc.setDoOutput(true);
- uc.setDoInput(true);
- uc.setUseCaches(false);
- uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- uc.connect();
- DataOutputStream out = new DataOutputStream(uc.getOutputStream());
- out.write(params.getBytes(gb));
- out.flush();
- out.close();
- InputStream inStream = uc.getInputStream(); // 获取文件流二进制数据
- ByteArrayOutputStream outStream = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int len = 0;
- while ((len = inStream.read(buffer)) != -1) {
- outStream.write(buffer, 0, len);
- }
- inStream.close();
- btFile = outStream.toByteArray();
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- } finally {
- if (uc != null) {
- uc.disconnect();
- }
- }
- return btFile;
- }
- /**
- * HTTP协议POST请求添加参数的封装方�?
- */
- private static String getParamStr(TreeMap<String, String> paramsMap) {
- StringBuilder param = new StringBuilder();
- for (Iterator<Map.Entry<String, String>> it = paramsMap.entrySet().iterator(); it.hasNext(); ) {
- Map.Entry<String, String> e = it.next();
- param.append("&").append(e.getKey()).append("=").append(e.getValue());
- }
- return param.toString().substring(1);
- }
- }
|