|
|
@@ -180,105 +180,4 @@ public class HttpClientUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// /** |
|
|
|
// * 发送 GET 请求 |
|
|
|
// * @param urlStr 请求 URL |
|
|
|
// * @param headers 请求头(可选) |
|
|
|
// * @return 响应内容 |
|
|
|
// * @throws IOException |
|
|
|
// */ |
|
|
|
// public static String doGet(String urlStr, Map<String, String> headers) throws IOException { |
|
|
|
// HttpURLConnection connection = null; |
|
|
|
// try { |
|
|
|
// // 创建 URL 对象 |
|
|
|
// URL url = new URL(urlStr); |
|
|
|
// connection = (HttpURLConnection) url.openConnection(); |
|
|
|
// connection.setRequestMethod("GET"); |
|
|
|
// connection.setConnectTimeout(CONNECT_TIMEOUT); |
|
|
|
// connection.setReadTimeout(CONNECT_TIMEOUT); |
|
|
|
// |
|
|
|
// // 设置请求头 |
|
|
|
// if (headers != null) { |
|
|
|
// for (Map.Entry<String, String> entry : headers.entrySet()) { |
|
|
|
// connection.setRequestProperty(entry.getKey(), entry.getValue()); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// |
|
|
|
// return getResponse(connection); |
|
|
|
// } finally { |
|
|
|
// if (connection != null) { |
|
|
|
// connection.disconnect(); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
// /** |
|
|
|
// * 发送 POST 请求 |
|
|
|
// * @param urlStr 请求 URL |
|
|
|
// * @param jsonBody JSON 格式的请求体 |
|
|
|
// * @param headers 请求头(可选) |
|
|
|
// * @return 响应内容 |
|
|
|
// * @throws IOException |
|
|
|
// */ |
|
|
|
// public static String doPost(String urlStr, String jsonBody, Map<String, String> headers) throws IOException { |
|
|
|
// HttpURLConnection connection = null; |
|
|
|
// try { |
|
|
|
// // 创建 URL 对象 |
|
|
|
// URL url = new URL(urlStr); |
|
|
|
// connection = (HttpURLConnection) url.openConnection(); |
|
|
|
// connection.setRequestMethod("POST"); |
|
|
|
// connection.setDoOutput(true); |
|
|
|
// connection.setConnectTimeout(CONNECT_TIMEOUT); |
|
|
|
// connection.setReadTimeout(CONNECT_TIMEOUT); |
|
|
|
// |
|
|
|
// // 设置请求头 |
|
|
|
// connection.setRequestProperty("Content-Type", "application/json"); |
|
|
|
// if (headers != null) { |
|
|
|
// for (Map.Entry<String, String> entry : headers.entrySet()) { |
|
|
|
// connection.setRequestProperty(entry.getKey(), entry.getValue()); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// |
|
|
|
// // 发送请求体 |
|
|
|
// if (jsonBody != null) { |
|
|
|
// try (OutputStream os = connection.getOutputStream()) { |
|
|
|
// byte[] input = jsonBody.getBytes(StandardCharsets.UTF_8); |
|
|
|
// os.write(input, 0, input.length); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// |
|
|
|
// return getResponse(connection); |
|
|
|
// } finally { |
|
|
|
// if (connection != null) { |
|
|
|
// connection.disconnect(); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
// /** |
|
|
|
// * 读取 HTTP 响应 |
|
|
|
// * @param connection HttpURLConnection 对象 |
|
|
|
// * @return 响应内容 |
|
|
|
// * @throws IOException |
|
|
|
// */ |
|
|
|
// private static String getResponse(HttpURLConnection connection) throws IOException { |
|
|
|
// int responseCode = connection.getResponseCode(); |
|
|
|
// InputStream inputStream = responseCode >= 200 && responseCode < 300 |
|
|
|
// ? connection.getInputStream() |
|
|
|
// : connection.getErrorStream(); |
|
|
|
// |
|
|
|
// if (inputStream == null) { |
|
|
|
// return ""; |
|
|
|
// } |
|
|
|
// |
|
|
|
// try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { |
|
|
|
// StringBuilder response = new StringBuilder(); |
|
|
|
// String line; |
|
|
|
// while ((line = reader.readLine()) != null) { |
|
|
|
// response.append(line); |
|
|
|
// } |
|
|
|
// return response.toString(); |
|
|
|
// } |
|
|
|
// } |
|
|
|
} |