WebUtils.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package com.welampiot.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.apache.http.client.config.RequestConfig;
  5. import org.apache.http.client.methods.CloseableHttpResponse;
  6. import org.apache.http.client.methods.HttpGet;
  7. import org.apache.http.impl.client.CloseableHttpClient;
  8. import org.apache.http.impl.client.HttpClients;
  9. import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.BufferedReader;
  12. import java.io.DataOutputStream;
  13. import java.io.IOException;
  14. import java.io.InputStreamReader;
  15. import java.net.HttpURLConnection;
  16. import java.net.URL;
  17. import java.net.URLConnection;
  18. import java.util.List;
  19. import java.util.Map;
  20. @Slf4j
  21. public class WebUtils
  22. {
  23. /**
  24. * 将字符串渲染到客户端
  25. *
  26. * @param response 渲染对象
  27. */
  28. public static void renderString(HttpServletResponse response, Object o) {
  29. try
  30. {
  31. response.setStatus(200);
  32. response.setContentType("application/json");
  33. response.setCharacterEncoding("utf-8");
  34. response.getWriter().print(JSON.toJSONString(o));
  35. }catch (IOException e){
  36. log.error("renderString error",e);
  37. }
  38. }
  39. /**
  40. * 将 HH:ii 格式的时间切换到某个时区的时间
  41. * @param time 时间 HH:ii
  42. * @param timezone 时区
  43. * @return 装换时区后的时间 HH:ii
  44. */
  45. public static String getDateFormat(String time,int timezone){
  46. String res = "";
  47. try {
  48. String[] split = time.split(":");
  49. int h = Integer.parseInt(split[0]);
  50. int i = Integer.parseInt(split[1]);
  51. if (h - timezone < 0) {
  52. h = h - timezone + 24;
  53. }else if(h - timezone >= 24){
  54. h = h - timezone - 24;
  55. }else{
  56. h = h - timezone;
  57. }
  58. res += ("00"+Integer.toHexString(h)).substring(-2);
  59. res += ("00"+Integer.toHexString(i)).substring(-2);
  60. }catch (Exception e){
  61. log.error("renderString error",e);
  62. }
  63. return res;
  64. }
  65. /**
  66. * 模拟post请求
  67. * @param url 请求地址
  68. * @param param 请求参数
  69. * @return 请求返回信息
  70. * @throws Exception
  71. */
  72. public static String requestPost(String url, String param) {
  73. StringBuilder response;
  74. try {
  75. URL obj = new URL(url);
  76. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  77. con.setRequestMethod("POST");
  78. con.setRequestProperty("User-Agent", "Mozilla/5.0");
  79. con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  80. con.setDoOutput(true);
  81. con.setReadTimeout(5000);
  82. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  83. wr.writeBytes(param);
  84. wr.flush();
  85. wr.close();
  86. BufferedReader in = new BufferedReader(
  87. new InputStreamReader(con.getInputStream()));
  88. String inputLine;
  89. response = new StringBuilder();
  90. while ((inputLine = in.readLine()) != null) {
  91. response.append(inputLine);
  92. }
  93. in.close();
  94. }catch (Exception e){
  95. return "";
  96. }
  97. return response.toString();
  98. }
  99. public static String requestGet(String url) {
  100. CloseableHttpResponse response = null;
  101. BufferedReader in = null;
  102. String result = "";
  103. PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
  104. cm.setMaxTotal(100);
  105. cm.setDefaultMaxPerRoute(20);
  106. cm.setDefaultMaxPerRoute(50);
  107. CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
  108. try {
  109. HttpGet httpGet = new HttpGet(url);
  110. RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
  111. httpGet.setConfig(requestConfig);
  112. httpGet.setConfig(requestConfig);
  113. httpGet.addHeader("Content-type", "application/json; charset=utf-8");
  114. httpGet.setHeader("Accept", "application/json");
  115. response = httpClient.execute(httpGet);
  116. in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  117. StringBuffer sb = new StringBuffer("");
  118. String line = "";
  119. String NL = System.getProperty("line.separator");
  120. while ((line = in.readLine()) != null) {
  121. sb.append(line + NL);
  122. }
  123. in.close();
  124. result = sb.toString();
  125. } catch (IOException e) {
  126. e.printStackTrace();
  127. } finally {
  128. try {
  129. if (null != response) {
  130. response.close();
  131. }
  132. } catch (IOException e) {
  133. e.printStackTrace();
  134. }
  135. }
  136. return result;
  137. }
  138. }