HttpUtils.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package cn.efunbox.base.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import javax.net.ssl.HttpsURLConnection;
  5. import javax.net.ssl.SSLContext;
  6. import javax.net.ssl.SSLSocketFactory;
  7. import javax.net.ssl.TrustManager;
  8. import java.io.*;
  9. import java.net.ConnectException;
  10. import java.net.URL;
  11. import java.util.UUID;
  12. public class HttpUtils {
  13. private static Logger LOGGER = LoggerFactory.getLogger(HttpUtils.class);
  14. public static String doRequest(String requestUrl, String requestMethod, String paramBody) {
  15. String log = "UUID = "+UUID.randomUUID() + " ; HttpUtils : ";
  16. LOGGER.info(log + " http weixin api request >>> request = " + requestUrl + " ;paramBody = " + paramBody);
  17. String result = null;
  18. try {
  19. HttpsURLConnection httpUrlConn = setSSLContext(requestUrl,requestMethod);
  20. // 当有数据需要提交时
  21. if (null != paramBody) {
  22. OutputStream outputStream = httpUrlConn.getOutputStream();
  23. // 注意编码格式,防止中文乱码
  24. outputStream.write(paramBody.getBytes("UTF-8"));
  25. outputStream.close();
  26. }
  27. result = connection2String(httpUrlConn);
  28. LOGGER.info(log + " http weixin api result >>> request = " + requestUrl + " ;paramBody = " + paramBody + " ;result" + result);
  29. } catch (ConnectException ce) {
  30. LOGGER.error(log + "Weixin request { " + requestUrl + " } connection timed out. paramBody : " + paramBody + ce.getMessage(),ce);
  31. } catch (Exception e) {
  32. LOGGER.error(log + "http request error:{} requestUrl : " + requestUrl + "paramBody : " + paramBody, e);
  33. }
  34. return result;
  35. }
  36. public static byte[] doRequestByte(String requestUrl, String requestMethod, String paramBody) {
  37. String log = "UUID = "+UUID.randomUUID() + " ; HttpUtils : ";
  38. LOGGER.info(log + " http weixin api request >>> request = " + requestUrl + " ;paramBody = " + paramBody);
  39. byte[] result = null;
  40. try {
  41. HttpsURLConnection httpUrlConn = setSSLContext(requestUrl,requestMethod);
  42. // 当有数据需要提交时
  43. if (null != paramBody) {
  44. OutputStream outputStream = httpUrlConn.getOutputStream();
  45. // 注意编码格式,防止中文乱码
  46. outputStream.write(paramBody.getBytes("UTF-8"));
  47. outputStream.close();
  48. }
  49. InputStream inputStream = httpUrlConn.getInputStream();
  50. ByteArrayOutputStream outstream = new ByteArrayOutputStream(httpUrlConn.getContentLength());
  51. byte[] buffer = new byte[4096];
  52. int len;
  53. while ((len = inputStream.read(buffer)) > 0) {
  54. outstream.write(buffer, 0, len);
  55. }
  56. outstream.close();
  57. result = outstream.toByteArray();
  58. LOGGER.info(log + " http weixin api result >>> request = " + requestUrl + " ;paramBody = " + paramBody );
  59. } catch (ConnectException ce) {
  60. LOGGER.error(log + "Weixin request { " + requestUrl + " } connection timed out. paramBody : " + paramBody + ce.getMessage(),ce);
  61. } catch (Exception e) {
  62. LOGGER.error(log + "http request error:{} requestUrl : " + requestUrl + "paramBody : " + paramBody, e);
  63. }
  64. return result;
  65. }
  66. private static String connection2String(HttpsURLConnection httpUrlConn) {
  67. StringBuffer buffer = new StringBuffer();
  68. // 将返回的输入流转换成字符串
  69. BufferedReader bufferedReader = null;
  70. try {
  71. bufferedReader = new BufferedReader(new InputStreamReader(httpUrlConn.getInputStream(), "utf-8"));
  72. String str = null;
  73. while ((str = bufferedReader.readLine()) != null) {
  74. buffer.append(str);
  75. }
  76. } catch (IOException e) {
  77. LOGGER.error("HttpsURLConnection2String is error," + e.getMessage(), e);
  78. } finally {
  79. if (bufferedReader != null) {
  80. try {
  81. bufferedReader.close();
  82. } catch (IOException e) {
  83. LOGGER.error("bufferedReader close error," + e.getMessage(), e);
  84. }
  85. }
  86. }
  87. httpUrlConn.disconnect();
  88. String result = buffer.toString();
  89. return result;
  90. }
  91. public static String doGet(String requestUrl, String paramBody) {
  92. return doRequest(requestUrl, "GET", paramBody);
  93. }
  94. public static String doPost(String requestUrl, String paramBody) {
  95. return doRequest(requestUrl, "POST", paramBody);
  96. }
  97. public static String doRequest(String requestUrl, String requestMethod) {
  98. String result = null;
  99. String log = "UUID = "+UUID.randomUUID();
  100. LOGGER.info(log + " http weixin api request >>> request = " + requestUrl);
  101. try {
  102. HttpsURLConnection httpUrlConn = setSSLContext(requestUrl,requestMethod);
  103. // 将返回的输入流转换成字符串
  104. result = connection2String(httpUrlConn);
  105. LOGGER.info(log + " http weixin api result >>> request = " + requestUrl + " ;result" + result);
  106. } catch (ConnectException ce) {
  107. LOGGER.error(log + " Weixin request {" + requestUrl + "} connection timed out." + ce.getMessage(),ce);
  108. } catch (Exception e) {
  109. LOGGER.error(log + " http request error:{} requestUrl :" + requestUrl + e.getMessage(), e);
  110. }
  111. return result;
  112. }
  113. private static HttpsURLConnection setSSLContext(String requestUrl,String requestMethod) throws Exception{
  114. HttpsURLConnection httpUrlConn = null;
  115. // 创建SSLContext对象,并使用我们指定的信任管理器初始化
  116. TrustManager[] tm = { new MyX509TrustManager() };
  117. SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
  118. sslContext.init(null, tm, new java.security.SecureRandom());
  119. // 从上述SSLContext对象中得到SSLSocketFactory对象
  120. SSLSocketFactory ssf = sslContext.getSocketFactory();
  121. URL url = new URL(requestUrl);
  122. httpUrlConn = (HttpsURLConnection) url.openConnection();
  123. httpUrlConn.setSSLSocketFactory(ssf);
  124. httpUrlConn.setDoOutput(true);
  125. httpUrlConn.setDoInput(true);
  126. httpUrlConn.setUseCaches(false);
  127. // 设置请求方式(GET/POST)
  128. httpUrlConn.setRequestMethod(requestMethod);
  129. if ("GET".equalsIgnoreCase(requestMethod)) {
  130. httpUrlConn.connect();
  131. }
  132. return httpUrlConn;
  133. }
  134. }