package com.yutou.tools.utils; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; import com.yutou.tools.utils.Interfaces.NetworkInterface; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.Set; public class HttpTools { public static String get(String url){ try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestProperty("User-Agent", getExtUa()); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder str = new StringBuilder(); String tmp; while ((tmp = reader.readLine()) != null) { str.append(tmp); } reader.close(); connection.disconnect(); return str.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } public static void post(final String url, final byte[] body, final NetworkInterface networkInterface) { new Thread(new Runnable() { @Override public void run() { String tmp; StringBuilder str = new StringBuilder(); try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.setConnectTimeout(5 * 1000); connection.setReadTimeout(10 * 1000); //connection.addRequestProperty("Connection", "keep-alive"); //connection.addRequestProperty("User-Agent", getExtUa()); //connection.addRequestProperty("content-type", "application/json"); connection.addRequestProperty("charset", "UTF-8"); OutputStream outputStream = connection.getOutputStream(); outputStream.write(body); outputStream.flush(); outputStream.close(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); while ((tmp = reader.readLine()) != null) { str.append(tmp); } final String finalStr = str.toString(); // Log.i(TAG + "[" + url + "?" + toGetSplice(body) + "]", "body:" + str + " (" + connection.getResponseCode() + ")"); if (networkInterface != null) { try { networkInterface.httpGetData(str.toString(), connection.getResponseCode()); } catch (IOException e) { e.printStackTrace(); } } connection.disconnect(); reader.close(); } catch (Exception e) { e.printStackTrace(); } } }).start(); } private static String getExtUa() { return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"; } public String toUrlParams(JSONObject json) { StringBuilder string = new StringBuilder(); Set keys = json.keySet(); for (String key : keys) { try { string.append("&").append(key).append("=").append(URLEncoder.encode(json.getString(key), "UTF-8")); } catch (Exception e) { e.printStackTrace(); try { string.append("&").append(URLEncoder.encode(key, "UTF-8")).append("="); // string += "&" + key + "="; } catch (Exception e1) { string.append("&").append(key).append("="); } } } string = new StringBuilder(string.substring(1, string.length()).replaceAll(" ", "")); return string.toString(); } }