package com.fr.plugin.oauth.utils; import com.fr.base.ServerConfig; import com.fr.json.JSONObject; import com.fr.third.fasterxml.jackson.core.JsonParseException; import com.fr.third.fasterxml.jackson.databind.DeserializationFeature; import com.fr.third.fasterxml.jackson.databind.JsonMappingException; import com.fr.third.fasterxml.jackson.databind.ObjectMapper; import javax.net.ssl.HttpsURLConnection; import javax.servlet.http.HttpServletRequest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.Iterator; import java.util.Map; import java.util.Set; public class HttpUtils { private static ObjectMapper mapper = new ObjectMapper(); static { mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); } public static String getRequestPayload(HttpServletRequest req) { StringBuilder sb = new StringBuilder(); try (BufferedReader reader = req.getReader();) { char[] buff = new char[1024]; int len; while ((len = reader.read(buff)) != -1) { sb.append(buff, 0, len); } } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } public static T json2Object(String json, Class typeRef) { try { return (T) mapper.readValue(json, typeRef); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { } return null; } public static String object2Json(Object ok) { try { return mapper.writeValueAsString(ok); } catch (JsonParseException e) { } catch (JsonMappingException e) { } catch (IOException e) { } return ""; } public JSONObject createResp(String uid, String reqId) { JSONObject json = new JSONObject(); json.put("uid", uid); json.put("bimRequestId", reqId); json.put("resultCode", "0"); json.put("message", "success"); return json; } private static String getParam(Map var0, String enc) { String var1 = ""; Set var2 = var0.keySet(); Iterator var3 = var2.iterator(); while (var3.hasNext()) { String var4 = (String) var3.next(); String var5 = var0.get(var4) + ""; try { var1 = var1 + (var1.length() == 0 ? "" : "&") + URLEncoder.encode(var4, enc) + "=" + URLEncoder.encode(var5, enc); } catch (Exception var7) { ; } } return var1; } public static boolean isHttps(URL url) { return url.getProtocol().toLowerCase().equals("https"); } public static String get(String path, Map param) { String paramStr = getParam(param, ServerConfig.getInstance().getServerCharset()); BufferedReader input = null; StringBuilder sb = null; URL url = null; HttpURLConnection con = null; try { url = new URL(path + (paramStr.length() > 0 ? "?" + paramStr.toString() : "")); if (isHttps(url)) { HttpsUtils.trustAllHosts(); HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection(); httpsCon.setHostnameVerifier(HttpsUtils.DO_NOT_VERIFY); con = httpsCon; } else { con = (HttpURLConnection) url.openConnection(); } con.setRequestProperty("accept", "*/*"); con.setRequestMethod("GET"); con.setRequestProperty("Accept-Charset", "UTF-8"); input = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8")); sb = new StringBuilder(); String s; while ((s = input.readLine()) != null) { sb.append(s).append("\n"); } } catch (Exception e) { e.printStackTrace(); } finally { // close buffered if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } // disconnecting releases the timescroller held by a connection so they may be closed or reused if (con != null) { con.disconnect(); } } return sb == null ? null : sb.toString(); } public static String post(String path, Map param) { String paramStr = getParam(param, ServerConfig.getInstance().getServerCharset()); PrintWriter writer = null; BufferedReader reader = null; HttpURLConnection con = null; String result = ""; try { URL url = new URL(path); if (isHttps(url)) { HttpsUtils.trustAllHosts(); HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection(); httpsCon.setHostnameVerifier(HttpsUtils.DO_NOT_VERIFY); con = httpsCon; } else { con = (HttpURLConnection) url.openConnection(); } con.setRequestProperty("accept", "*/*"); con.setRequestProperty("connection", "Keep-Alive"); //con.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // var8.setRequestProperty("Accept-Charset", "UTF-8"); con.setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); writer = new PrintWriter(con.getOutputStream()); writer.print(paramStr); writer.flush(); String line; for (reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); (line = reader.readLine()) != null; result = result + line) { ; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (writer != null) { writer.close(); } if (reader != null) { reader.close(); } } catch (Exception var17) { ; } } return result; } }