package com.welampiot.utils; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; @Slf4j public class WebUtils { /** * 将字符串渲染到客户端 * * @param response 渲染对象 */ public static void renderString(HttpServletResponse response, Object o) { try { response.setStatus(200); response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); response.getWriter().print(JSON.toJSONString(o)); }catch (IOException e){ log.error("renderString error",e); } } /** * 将 HH:ii 格式的时间切换到某个时区的时间 * @param time 时间 HH:ii * @param timezone 时区 * @return 装换时区后的时间 HH:ii */ public static String getDateFormat(String time,int timezone){ String res = ""; try { String[] split = time.split(":"); int h = Integer.parseInt(split[0]); int i = Integer.parseInt(split[1]); if (h - timezone < 0) { h = h - timezone + 24; }else if(h - timezone >= 24){ h = h - timezone - 24; }else{ h = h - timezone; } res += ("00"+Integer.toHexString(h)).substring(-2); res += ("00"+Integer.toHexString(i)).substring(-2); }catch (Exception e){ log.error("renderString error",e); } return res; } /** * 模拟post请求 * @param url 请求地址 * @param param 请求参数 * @return 请求返回信息 * @throws Exception */ public static String requestPost(String url, String param) { StringBuilder response; try { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); con.setDoOutput(true); con.setReadTimeout(5000); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(param); wr.flush(); wr.close(); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); }catch (Exception e){ return ""; } return response.toString(); } public static String requestGet(String url) { CloseableHttpResponse response = null; BufferedReader in = null; String result = ""; PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(100); cm.setDefaultMaxPerRoute(20); cm.setDefaultMaxPerRoute(50); CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build(); try { HttpGet httpGet = new HttpGet(url); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build(); httpGet.setConfig(requestConfig); httpGet.setConfig(requestConfig); httpGet.addHeader("Content-type", "application/json; charset=utf-8"); httpGet.setHeader("Accept", "application/json"); response = httpClient.execute(httpGet); in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } in.close(); result = sb.toString(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != response) { response.close(); } } catch (IOException e) { e.printStackTrace(); } } return result; } }