110 lines
3.6 KiB
Java
110 lines
3.6 KiB
Java
package org.example;
|
||
|
||
/**
|
||
* packageName com.example.demo
|
||
*
|
||
* @author congtiexin
|
||
* @date 2025/6/3
|
||
* @description TODO
|
||
*/
|
||
|
||
import com.google.gson.Gson;
|
||
|
||
import java.io.BufferedReader;
|
||
import java.io.InputStreamReader;
|
||
import java.io.OutputStream;
|
||
import java.net.HttpURLConnection;
|
||
import java.net.URL;
|
||
|
||
public class HttpUtils {
|
||
|
||
private static final Gson gson = new Gson();
|
||
|
||
/**
|
||
* 发送 POST 请求并返回 JSON 响应
|
||
*
|
||
* @param url 请求地址
|
||
* @param request 请求对象(Java Bean 或 Map)
|
||
* @return 响应内容(Map 或 自定义对象)
|
||
*/
|
||
public static <T> T postJson(String url, Object request, Class<T> responseType) throws Exception {
|
||
HttpURLConnection connection = null;
|
||
try {
|
||
// 初始化连接
|
||
connection = (HttpURLConnection) new URL(url).openConnection();
|
||
connection.setRequestMethod("POST");
|
||
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
||
connection.setRequestProperty("Accept", "application/json");
|
||
connection.setDoOutput(true);
|
||
|
||
// 序列化请求体
|
||
String jsonInput = gson.toJson(request);
|
||
try (OutputStream os = connection.getOutputStream()) {
|
||
os.write(jsonInput.getBytes("UTF-8"));
|
||
os.flush();
|
||
}
|
||
|
||
// 处理响应
|
||
int responseCode = connection.getResponseCode();
|
||
if (responseCode != HttpURLConnection.HTTP_OK) {
|
||
throw new RuntimeException("请求失败,状态码: " + responseCode);
|
||
}
|
||
|
||
StringBuilder response = new StringBuilder();
|
||
try (BufferedReader br = new BufferedReader(
|
||
new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
|
||
String line;
|
||
while ((line = br.readLine()) != null) {
|
||
response.append(line);
|
||
}
|
||
}
|
||
|
||
// 反序列化响应
|
||
return gson.fromJson(response.toString(), responseType);
|
||
} finally {
|
||
if (connection != null) {
|
||
connection.disconnect();
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 快速发送 JSON POST 请求并返回原始字符串响应
|
||
*/
|
||
public static String postJson(String url, String jsonBody) throws Exception {
|
||
HttpURLConnection connection = null;
|
||
try {
|
||
connection = (HttpURLConnection) new URL(url).openConnection();
|
||
connection.setRequestMethod("POST");
|
||
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
||
connection.setDoOutput(true);
|
||
|
||
try (OutputStream os = connection.getOutputStream()) {
|
||
byte[] input = jsonBody.getBytes("UTF-8");
|
||
os.write(input, 0, input.length);
|
||
os.flush();
|
||
}
|
||
|
||
int responseCode = connection.getResponseCode();
|
||
if (responseCode != HttpURLConnection.HTTP_OK) {
|
||
throw new RuntimeException("请求失败,状态码: " + responseCode);
|
||
}
|
||
|
||
StringBuilder response = new StringBuilder();
|
||
try (BufferedReader br = new BufferedReader(
|
||
new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
|
||
String line;
|
||
while ((line = br.readLine()) != null) {
|
||
response.append(line);
|
||
}
|
||
}
|
||
|
||
return response.toString();
|
||
} finally {
|
||
if (connection != null) {
|
||
connection.disconnect();
|
||
}
|
||
}
|
||
}
|
||
}
|