HttpUtil.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package cn.com.qmth.mps.util;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStream;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.security.cert.CertificateException;
  10. import java.security.cert.X509Certificate;
  11. import java.util.Map;
  12. import javax.net.ssl.HttpsURLConnection;
  13. import javax.net.ssl.SSLContext;
  14. import javax.net.ssl.SSLSocketFactory;
  15. import javax.net.ssl.TrustManager;
  16. import javax.net.ssl.X509TrustManager;
  17. import com.qmth.boot.core.exception.StatusException;
  18. public class HttpUtil {
  19. /** 默认的编码格式 */
  20. private static final String DEFAULT_CHARSET = "UTF-8";
  21. private static final String CONTENT_TYPE = "Content-Type";
  22. private static final String APPLICATION_FORM= "application/x-www-form-urlencoded;charset=utf-8";
  23. private static final String APPLICATION_JSON = "application/json;charset=utf-8";
  24. public static String httpActionPost(String uri, Map<String, String> heads, Map<String, String> params) {
  25. String result = null;
  26. HttpsURLConnection conn = null;
  27. OutputStream os = null;
  28. InputStream is = null;
  29. try {
  30. // 获取链接
  31. URL url = new URL(uri);
  32. conn = (HttpsURLConnection) url.openConnection();
  33. conn.setRequestMethod("POST");
  34. conn.setRequestProperty(CONTENT_TYPE, APPLICATION_JSON);
  35. // ssl
  36. SSLContext context = SSLContext.getInstance("SSL", "SunJSSE");
  37. TrustManager[] tm = new TrustManager[] { new X509TrustManager() {
  38. @Override
  39. public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  40. }
  41. @Override
  42. public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  43. }
  44. @Override
  45. public X509Certificate[] getAcceptedIssuers() {
  46. return null;
  47. }
  48. } };
  49. // 初始化
  50. context.init(null, tm, new java.security.SecureRandom());
  51. // 获取SSLSocketFactory对象
  52. SSLSocketFactory ssf = context.getSocketFactory();
  53. conn.setSSLSocketFactory(ssf);
  54. conn.setUseCaches(false);
  55. conn.setDoOutput(true);
  56. // 设置额外的参数
  57. if (heads != null && !heads.isEmpty()) {
  58. for (Map.Entry<String, String> head : heads.entrySet()) {
  59. conn.setRequestProperty(head.getKey(), head.getValue());
  60. }
  61. }
  62. // 创建链接
  63. conn.connect();
  64. if (params != null) {
  65. StringBuilder sb = new StringBuilder();
  66. for (Map.Entry<String, String> data : params.entrySet()) {
  67. sb.append(data.getKey()).append("=").append(data.getValue()).append("&");
  68. }
  69. os = conn.getOutputStream();
  70. os.write(sb.toString().getBytes());
  71. os.flush();
  72. }
  73. result = getResult(conn);
  74. } catch (Exception e) {
  75. throw new StatusException("授权服务器访问失败", e);
  76. } finally {
  77. try {
  78. if (os != null) {
  79. os.close();
  80. os = null;
  81. }
  82. if (is != null) {
  83. is.close();
  84. is = null;
  85. }
  86. } catch (IOException e) {
  87. }
  88. if (conn != null) {
  89. conn.disconnect();
  90. conn = null;
  91. }
  92. }
  93. return result;
  94. }
  95. /**
  96. *
  97. * @param params headers参数
  98. * @param datas requestParams参数
  99. * @return
  100. * @throws Exception
  101. */
  102. public static String httpActionGet(String uri, Map<String, String> heads, Map<String, String> params) {
  103. String result = null;
  104. HttpsURLConnection conn = null;
  105. OutputStream os = null;
  106. InputStream is = null;
  107. try {
  108. // 设置请求参数
  109. if (params != null) {
  110. StringBuilder sb = new StringBuilder();
  111. for (Map.Entry<String, String> data : params.entrySet()) {
  112. sb.append(data.getKey()).append("=").append(data.getValue()).append("&");
  113. }
  114. uri = uri+"?" + sb.toString();
  115. }
  116. // 获取链接
  117. URL url = new URL(uri);
  118. conn = (HttpsURLConnection) url.openConnection();
  119. conn.setRequestMethod("GET");
  120. conn.setRequestProperty(CONTENT_TYPE, APPLICATION_FORM);
  121. // ssl
  122. SSLContext context = SSLContext.getInstance("SSL", "SunJSSE");
  123. TrustManager[] tm = new TrustManager[] { new X509TrustManager() {
  124. @Override
  125. public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  126. }
  127. @Override
  128. public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  129. }
  130. @Override
  131. public X509Certificate[] getAcceptedIssuers() {
  132. return null;
  133. }
  134. } };
  135. // 初始化
  136. context.init(null, tm, new java.security.SecureRandom());
  137. // 获取SSLSocketFactory对象
  138. SSLSocketFactory ssf = context.getSocketFactory();
  139. conn.setSSLSocketFactory(ssf);
  140. conn.setUseCaches(false);
  141. conn.setDoOutput(true);
  142. // 设置额外的参数
  143. if (heads != null && !heads.isEmpty()) {
  144. for (Map.Entry<String, String> head : heads.entrySet()) {
  145. conn.setRequestProperty(head.getKey(), head.getValue());
  146. }
  147. }
  148. // 创建链接
  149. conn.connect();
  150. result = getResult(conn);
  151. } catch (Exception e) {
  152. throw new StatusException("授权服务器访问失败", e);
  153. } finally {
  154. try {
  155. if (os != null) {
  156. os.close();
  157. os = null;
  158. }
  159. if (is != null) {
  160. is.close();
  161. is = null;
  162. }
  163. } catch (IOException e) {
  164. }
  165. if (conn != null) {
  166. conn.disconnect();
  167. conn = null;
  168. }
  169. }
  170. return result;
  171. }
  172. /**
  173. * 获得连接请求的返回数据
  174. *
  175. * @param conn
  176. *
  177. * @return 字符串
  178. */
  179. private static String getResult(HttpURLConnection conn) throws IOException {
  180. StringBuilder text = new StringBuilder();
  181. InputStream is = null;
  182. InputStreamReader sr = null;
  183. BufferedReader br = null;
  184. int code = conn.getResponseCode();
  185. try {
  186. is = code != 200 ? conn.getErrorStream() : conn.getInputStream();
  187. sr = new InputStreamReader(is, DEFAULT_CHARSET);
  188. br = new BufferedReader(sr);
  189. char[] chars = new char[4096];
  190. int length = 0;
  191. while ((length = br.read(chars)) != -1) {
  192. text.append(chars, 0, length);
  193. }
  194. } finally {
  195. if (br != null) {
  196. br.close();
  197. br = null;
  198. }
  199. if (sr != null) {
  200. sr.close();
  201. sr = null;
  202. }
  203. if (is != null) {
  204. is.close();
  205. is = null;
  206. }
  207. }
  208. if (code != 200) {
  209. throw new IOException(text.toString());
  210. }
  211. return text.toString();
  212. }
  213. }