HttpsUtil.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. * @Filename:HttpsUtil.java
  3. * @Author:caiqf
  4. * @Date�?013-9-23
  5. */
  6. package com.happy.Unitil_nsh;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import javax.net.ssl.*;
  10. import java.io.*;
  11. import java.net.URL;
  12. import java.security.cert.CertificateException;
  13. import java.security.cert.X509Certificate;
  14. import java.util.Iterator;
  15. import java.util.Map;
  16. import java.util.TreeMap;
  17. import java.util.zip.GZIPInputStream;
  18. /**
  19. * @Class:HttpsUtil.java
  20. * @Description�?
  21. * @Author:caiqf
  22. * @Date�?013-9-23
  23. */
  24. public class HttpsUtil {
  25. private static final Log log = LogFactory.getLog(HttpsUtil.class);
  26. private static class MyTrustManager implements X509TrustManager {
  27. public void checkClientTrusted(X509Certificate[] chain, String authType)
  28. throws CertificateException {
  29. }
  30. public void checkServerTrusted(X509Certificate[] chain, String authType)
  31. throws CertificateException {
  32. }
  33. public X509Certificate[] getAcceptedIssuers() {
  34. return new X509Certificate[]{};
  35. }
  36. }
  37. private static class MyHostnameVerifier implements HostnameVerifier {
  38. public boolean verify(String hostname, SSLSession session) {
  39. return true;
  40. }
  41. }
  42. /**
  43. * HTTP协议GET请求方法
  44. */
  45. public static String httpMethodGet(String url, String gb) {
  46. if (null == gb || "".equals(gb)) {
  47. gb = "UTF-8";
  48. }
  49. StringBuffer sb = new StringBuffer();
  50. URL urls;
  51. HttpsURLConnection uc = null;
  52. BufferedReader in = null;
  53. try {
  54. SSLContext sc = SSLContext.getInstance("SSL");
  55. sc.init(null, new TrustManager[]{new MyTrustManager()}, new java.security.SecureRandom());
  56. urls = new URL(url);
  57. uc = (HttpsURLConnection) urls.openConnection();
  58. uc.setSSLSocketFactory(sc.getSocketFactory());
  59. uc.setHostnameVerifier(new MyHostnameVerifier());
  60. uc.setRequestMethod("GET");
  61. uc.connect();
  62. in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "utf-8"));
  63. String readLine = "";
  64. while ((readLine = in.readLine()) != null) {
  65. sb.append(readLine);
  66. }
  67. if (in != null) {
  68. in.close();
  69. }
  70. if (uc != null) {
  71. uc.disconnect();
  72. }
  73. } catch (Exception e) {
  74. log.error(e.getMessage(), e);
  75. } finally {
  76. if (uc != null) {
  77. uc.disconnect();
  78. }
  79. }
  80. return sb.toString();
  81. }
  82. /**
  83. * HTTP协议GET请求方法
  84. */
  85. public static byte[] httpMethodGetFile(String url) {
  86. byte[] btFile = null;
  87. URL urls = null;
  88. HttpsURLConnection uc = null;
  89. try {
  90. SSLContext sc = SSLContext.getInstance("SSL");
  91. sc.init(null, new TrustManager[]{new MyTrustManager()},
  92. new java.security.SecureRandom());
  93. urls = new URL(url);
  94. uc = (HttpsURLConnection) urls.openConnection();
  95. uc.setSSLSocketFactory(sc.getSocketFactory());
  96. uc.setHostnameVerifier(new MyHostnameVerifier());
  97. uc.setRequestMethod("GET");
  98. uc.connect();
  99. InputStream inStream = uc.getInputStream(); // 获取文件流二进制数据
  100. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  101. byte[] buffer = new byte[1024];
  102. int len = 0;
  103. while ((len = inStream.read(buffer)) != -1) {
  104. outStream.write(buffer, 0, len);
  105. }
  106. inStream.close();
  107. btFile = outStream.toByteArray();
  108. } catch (Exception e) {
  109. log.error(e.getMessage(), e);
  110. } finally {
  111. if (uc != null) {
  112. uc.disconnect();
  113. }
  114. }
  115. return btFile;
  116. }
  117. /**
  118. * HTTP协议POST请求方法
  119. */
  120. public static String httpMethodPost(String url, String params, String gb) {
  121. if (null == gb || "".equals(gb)) {
  122. gb = "UTF-8";
  123. }
  124. StringBuffer sb = new StringBuffer();
  125. URL urls;
  126. HttpsURLConnection uc = null;
  127. BufferedReader in = null;
  128. try {
  129. SSLContext sc = SSLContext.getInstance("SSL");
  130. sc.init(null, new TrustManager[]{new MyTrustManager()}, new java.security.SecureRandom());
  131. urls = new URL(url);
  132. uc = (HttpsURLConnection) urls.openConnection();
  133. uc.setSSLSocketFactory(sc.getSocketFactory());
  134. uc.setHostnameVerifier(new MyHostnameVerifier());
  135. uc.setRequestMethod("POST");
  136. uc.setDoOutput(true);
  137. uc.setDoInput(true);
  138. uc.setUseCaches(false);
  139. uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  140. uc.connect();
  141. DataOutputStream out = new DataOutputStream(uc.getOutputStream());
  142. out.write(params.getBytes(gb));
  143. out.flush();
  144. out.close();
  145. in = new BufferedReader(new InputStreamReader(uc.getInputStream(), gb));
  146. String readLine = "";
  147. while ((readLine = in.readLine()) != null) {
  148. sb.append(readLine);
  149. }
  150. if (in != null) {
  151. in.close();
  152. }
  153. if (uc != null) {
  154. uc.disconnect();
  155. }
  156. } catch (Exception e) {
  157. log.error(e.getMessage(), e);
  158. } finally {
  159. if (uc != null) {
  160. uc.disconnect();
  161. }
  162. }
  163. return sb.toString();
  164. }
  165. /**
  166. * HTTP协议POST请求方法
  167. */
  168. public static byte[] httpMethodPostFile(String url, String params, String gb) {
  169. if (null == gb || "".equals(gb)) {
  170. gb = "UTF-8";
  171. }
  172. byte[] btFile = null;
  173. URL urls = null;
  174. HttpsURLConnection uc = null;
  175. try {
  176. SSLContext sc = SSLContext.getInstance("SSL");
  177. sc.init(null, new TrustManager[]{new MyTrustManager()}, new java.security.SecureRandom());
  178. urls = new URL(url);
  179. uc = (HttpsURLConnection) urls.openConnection();
  180. uc.setSSLSocketFactory(sc.getSocketFactory());
  181. uc.setHostnameVerifier(new MyHostnameVerifier());
  182. uc.setRequestMethod("POST");
  183. uc.setDoOutput(true);
  184. uc.setDoInput(true);
  185. uc.setUseCaches(false);
  186. uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  187. uc.connect();
  188. DataOutputStream out = new DataOutputStream(uc.getOutputStream());
  189. out.write(params.getBytes(gb));
  190. out.flush();
  191. out.close();
  192. InputStream inStream = uc.getInputStream(); // 获取文件流二进制数据
  193. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  194. byte[] buffer = new byte[1024];
  195. int len = 0;
  196. while ((len = inStream.read(buffer)) != -1) {
  197. outStream.write(buffer, 0, len);
  198. }
  199. inStream.close();
  200. btFile = outStream.toByteArray();
  201. } catch (Exception e) {
  202. log.error(e.getMessage(), e);
  203. } finally {
  204. if (uc != null) {
  205. uc.disconnect();
  206. }
  207. }
  208. return btFile;
  209. }
  210. /**
  211. * HTTP协议POST请求方法
  212. */
  213. public static String httpMethodPost(String url, TreeMap<String, String> paramsMap, String gb) {
  214. if (null == gb || "".equals(gb)) {
  215. gb = "UTF-8";
  216. }
  217. String params = null;
  218. if (null != paramsMap) {
  219. params = getParamStr(paramsMap);
  220. }
  221. System.out.println("====post请求参数= " + params);
  222. StringBuffer sb = new StringBuffer();
  223. URL urls;
  224. HttpsURLConnection uc = null;
  225. BufferedReader in = null;
  226. try {
  227. SSLContext sc = SSLContext.getInstance("SSL");
  228. sc.init(null, new TrustManager[]{new MyTrustManager()}, new java.security.SecureRandom());
  229. urls = new URL(url);
  230. uc = (HttpsURLConnection) urls.openConnection();
  231. uc.setSSLSocketFactory(sc.getSocketFactory());
  232. uc.setHostnameVerifier(new MyHostnameVerifier());
  233. uc.setRequestMethod("POST");
  234. uc.setDoOutput(true);
  235. uc.setDoInput(true);
  236. uc.setUseCaches(false);
  237. uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  238. uc.connect();
  239. DataOutputStream out = new DataOutputStream(uc.getOutputStream());
  240. out.write(params.getBytes(gb));
  241. out.flush();
  242. out.close();
  243. in = new BufferedReader(new InputStreamReader(uc.getInputStream(), gb));
  244. String readLine = "";
  245. while ((readLine = in.readLine()) != null) {
  246. sb.append(readLine);
  247. }
  248. if (in != null) {
  249. in.close();
  250. }
  251. if (uc != null) {
  252. uc.disconnect();
  253. }
  254. } catch (Exception e) {
  255. log.error(e.getMessage(), e);
  256. } finally {
  257. if (uc != null) {
  258. uc.disconnect();
  259. }
  260. }
  261. return sb.toString();
  262. }
  263. public static String httpMethodPostGZIP(String url, TreeMap<String, String> paramsMap, String gb) {
  264. if (null == gb || "".equals(gb)) {
  265. gb = "UTF-8";
  266. }
  267. String params = null;
  268. if (null != paramsMap) {
  269. params = getParamStr(paramsMap);
  270. }
  271. System.out.println("==================post请求地址:" + url);
  272. System.out.println("==================post请求参数:" + params);
  273. StringBuffer sb = new StringBuffer();
  274. URL urls;
  275. HttpsURLConnection uc = null;
  276. BufferedReader in = null;
  277. try {
  278. SSLContext sc = SSLContext.getInstance("SSL");
  279. sc.init(null, new TrustManager[]{new MyTrustManager()}, new java.security.SecureRandom());
  280. urls = new URL(url);
  281. uc = (HttpsURLConnection) urls.openConnection();
  282. uc.setSSLSocketFactory(sc.getSocketFactory());
  283. uc.setHostnameVerifier(new MyHostnameVerifier());
  284. uc.setRequestMethod("POST");
  285. uc.setDoOutput(true);
  286. uc.setDoInput(true);
  287. uc.setUseCaches(false);
  288. uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  289. uc.connect();
  290. DataOutputStream out = new DataOutputStream(uc.getOutputStream());
  291. out.write(params.getBytes(gb));
  292. out.flush();
  293. out.close();
  294. in = new BufferedReader(new InputStreamReader(new GZIPInputStream(uc.getInputStream()), gb));
  295. String readLine = "";
  296. while ((readLine = in.readLine()) != null) {
  297. sb.append(readLine).append("\n");
  298. }
  299. if (in != null) {
  300. in.close();
  301. }
  302. if (uc != null) {
  303. uc.disconnect();
  304. }
  305. } catch (Exception e) {
  306. log.error(e.getMessage(), e);
  307. } finally {
  308. if (uc != null) {
  309. uc.disconnect();
  310. }
  311. }
  312. return sb.toString();
  313. }
  314. /**
  315. * HTTP协议POST请求方法
  316. */
  317. public static byte[] httpMethodPostFile(String url, TreeMap<String, String> paramsMap, String gb) {
  318. if (null == gb || "".equals(gb)) {
  319. gb = "UTF-8";
  320. }
  321. String params = null;
  322. if (null != paramsMap) {
  323. params = getParamStr(paramsMap);
  324. }
  325. byte[] btFile = null;
  326. URL urls = null;
  327. HttpsURLConnection uc = null;
  328. try {
  329. SSLContext sc = SSLContext.getInstance("SSL");
  330. sc.init(null, new TrustManager[]{new MyTrustManager()}, new java.security.SecureRandom());
  331. urls = new URL(url);
  332. uc = (HttpsURLConnection) urls.openConnection();
  333. uc.setSSLSocketFactory(sc.getSocketFactory());
  334. uc.setHostnameVerifier(new MyHostnameVerifier());
  335. uc.setRequestMethod("POST");
  336. uc.setDoOutput(true);
  337. uc.setDoInput(true);
  338. uc.setUseCaches(false);
  339. uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  340. uc.connect();
  341. DataOutputStream out = new DataOutputStream(uc.getOutputStream());
  342. out.write(params.getBytes(gb));
  343. out.flush();
  344. out.close();
  345. InputStream inStream = uc.getInputStream(); // 获取文件流二进制数据
  346. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  347. byte[] buffer = new byte[1024];
  348. int len = 0;
  349. while ((len = inStream.read(buffer)) != -1) {
  350. outStream.write(buffer, 0, len);
  351. }
  352. inStream.close();
  353. btFile = outStream.toByteArray();
  354. } catch (Exception e) {
  355. log.error(e.getMessage(), e);
  356. } finally {
  357. if (uc != null) {
  358. uc.disconnect();
  359. }
  360. }
  361. return btFile;
  362. }
  363. /**
  364. * HTTP协议POST请求添加参数的封装方�?
  365. */
  366. private static String getParamStr(TreeMap<String, String> paramsMap) {
  367. StringBuilder param = new StringBuilder();
  368. for (Iterator<Map.Entry<String, String>> it = paramsMap.entrySet().iterator(); it.hasNext(); ) {
  369. Map.Entry<String, String> e = it.next();
  370. param.append("&").append(e.getKey()).append("=").append(e.getValue());
  371. }
  372. return param.toString().substring(1);
  373. }
  374. }