package com.ruoyi.system.ControllerUtil; import com.alibaba.fastjson2.JSONObject; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.StringJoiner; /** * 通用第三方接口请求工具类 * 支持GET和POST请求,支持自定义Header、请求体、URL参数等 * 可扩展、可复用,适用于各种第三方接口调用场景 */ public class PublicSendApiUtil { /** * 发送GET请求 * @param urlStr 请求地址 * @param headers 请求头参数(可为null) * @param params URL参数(可为null) * @return 响应内容 * @throws Exception 异常 */ public static String sendGet(String urlStr, Map headers, Map params) throws Exception { // 拼接URL参数 if (params != null && !params.isEmpty()) { StringJoiner sj = new StringJoiner("&"); for (Map.Entry entry : params.entrySet()) { sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8")); } urlStr += urlStr.contains("?") ? "&" + sj.toString() : "?" + sj.toString(); } URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); // 设置请求头 if (headers != null) { for (Map.Entry entry : headers.entrySet()) { conn.setRequestProperty(entry.getKey(), entry.getValue()); } } conn.setDoInput(true); // 读取响应 int code = conn.getResponseCode(); InputStream is = (code == 200) ? conn.getInputStream() : conn.getErrorStream(); StringBuilder sb = new StringBuilder(); byte[] buf = new byte[1024]; int len; while ((len = is.read(buf)) != -1) { sb.append(new String(buf, 0, len, StandardCharsets.UTF_8)); } is.close(); return sb.toString(); } /** * 发送POST请求 * @param urlStr 请求地址 * @param headers 请求头参数(可为null) * @param params URL参数(可为null) * @param body 请求体内容(可为null,通常为JSON字符串或表单数据) * @return 响应内容 * @throws Exception 异常 */ public static String sendPost(String urlStr, Map headers, Map params, String body) throws Exception { // 拼接URL参数 if (params != null && !params.isEmpty()) { StringJoiner sj = new StringJoiner("&"); for (Map.Entry entry : params.entrySet()) { sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + URLEncoder.encode(entry.getValue(), "UTF-8")); } urlStr += urlStr.contains("?") ? "&" + sj.toString() : "?" + sj.toString(); } URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); // 设置请求头 if (headers != null) { for (Map.Entry entry : headers.entrySet()) { conn.setRequestProperty(entry.getKey(), entry.getValue()); } } conn.setDoOutput(true); conn.setDoInput(true); // 写入请求体 if (body != null) { try (OutputStream os = conn.getOutputStream()) { os.write(body.getBytes(StandardCharsets.UTF_8)); } } System.out.println(conn); // 读取响应 int code = conn.getResponseCode(); InputStream is = (code == 000000) ? conn.getInputStream() : conn.getErrorStream(); StringBuilder sb = new StringBuilder(); byte[] buf = new byte[1024]; int len; while ((len = is.read(buf)) != -1) { sb.append(new String(buf, 0, len, StandardCharsets.UTF_8)); } is.close(); return sb.toString(); } /** * 示例:如何使用该工具类发送GET和POST请求 */ public static void main(String[] args) throws Exception { // GET请求示例 Map headers = new HashMap<>(); headers.put("Accept", "application/json"); Map params = new HashMap<>(); params.put("q", "test"); String getResult = sendGet("https://httpbin.org/get", headers, params); System.out.println("GET结果:" + getResult); // POST请求示例 headers.put("Content-Type", "application/json"); String postBody = "{\"name\":\"张三\"}"; String postResult = sendPost("https://httpbin.org/post", headers, null, postBody); System.out.println("POST结果:" + postResult); } }