4 changed files with 164 additions and 29 deletions
-
132src/main/java/com/gaotao/common/utils/HttpUtils.java
-
3src/main/java/com/gaotao/modules/po/controller/PoController.java
-
3src/main/java/com/gaotao/modules/po/service/PoService.java
-
55src/main/java/com/gaotao/modules/po/service/impl/PoServiceImpl.java
@ -0,0 +1,132 @@ |
|||||
|
package com.gaotao.common.utils; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
import java.net.URI; |
||||
|
import java.net.URLEncoder; |
||||
|
import java.net.http.HttpClient; |
||||
|
import java.net.http.HttpRequest; |
||||
|
import java.net.http.HttpResponse; |
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
import java.time.Duration; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
public class HttpUtils { |
||||
|
|
||||
|
private static final HttpClient client = HttpClient.newBuilder() |
||||
|
.connectTimeout(Duration.ofSeconds(30)) |
||||
|
.build(); |
||||
|
|
||||
|
/** |
||||
|
* 发送 GET 请求,支持 Query 参数 |
||||
|
* |
||||
|
* @param url 请求 URL |
||||
|
* @param params 查询参数(可为 null) |
||||
|
* @param headers 请求头(可为 null) |
||||
|
* @return 响应字符串 |
||||
|
*/ |
||||
|
public static String doGet(String url, Map<String, String> params, Map<String, String> headers) { |
||||
|
try { |
||||
|
if (params != null && !params.isEmpty()) { |
||||
|
String query = buildQueryString(params); |
||||
|
url = url + "?" + query; |
||||
|
} |
||||
|
|
||||
|
HttpRequest.Builder builder = HttpRequest.newBuilder() |
||||
|
.uri(URI.create(url)) |
||||
|
.timeout(Duration.ofSeconds(60)) |
||||
|
.GET(); |
||||
|
|
||||
|
if (headers != null) { |
||||
|
headers.forEach(builder::header); |
||||
|
} |
||||
|
|
||||
|
HttpResponse<String> response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString()); |
||||
|
return handleResponse(response); |
||||
|
} catch (IOException | InterruptedException e) { |
||||
|
Thread.currentThread().interrupt(); |
||||
|
throw new RuntimeException("GET 请求失败: " + e.getMessage(), e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 发送 GET 请求,携带 JSON Body(非标准,谨慎使用) |
||||
|
* |
||||
|
* @param url 请求 URL |
||||
|
* @param jsonBody JSON 字符串 |
||||
|
* @param headers 请求头(可为 null) |
||||
|
* @return 响应字符串 |
||||
|
*/ |
||||
|
public static String doGetWithBody(String url, String jsonBody, Map<String, String> headers) { |
||||
|
try { |
||||
|
HttpRequest.Builder builder = HttpRequest.newBuilder() |
||||
|
.uri(URI.create(url)) |
||||
|
.timeout(Duration.ofSeconds(60)) |
||||
|
.method("GET", HttpRequest.BodyPublishers.ofString(jsonBody)) |
||||
|
.header("Content-Type", "application/json"); |
||||
|
|
||||
|
if (headers != null) { |
||||
|
headers.forEach(builder::header); |
||||
|
} |
||||
|
|
||||
|
HttpResponse<String> response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString()); |
||||
|
return handleResponse(response); |
||||
|
} catch (IOException | InterruptedException e) { |
||||
|
Thread.currentThread().interrupt(); |
||||
|
throw new RuntimeException("GET (JSON Body) 请求失败: " + e.getMessage(), e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 发送 POST 请求,JSON Body |
||||
|
* |
||||
|
* @param url 请求 URL |
||||
|
* @param jsonBody JSON 字符串 |
||||
|
* @param headers 请求头(可为 null) |
||||
|
* @return 响应字符串 |
||||
|
*/ |
||||
|
public static String doPost(String url, String jsonBody, Map<String, String> headers) { |
||||
|
try { |
||||
|
HttpRequest.Builder builder = HttpRequest.newBuilder() |
||||
|
.uri(URI.create(url)) |
||||
|
.timeout(Duration.ofSeconds(60)) |
||||
|
.POST(HttpRequest.BodyPublishers.ofString(jsonBody)) |
||||
|
.header("Content-Type", "application/json"); |
||||
|
|
||||
|
if (headers != null) { |
||||
|
headers.forEach(builder::header); |
||||
|
} |
||||
|
|
||||
|
HttpResponse<String> response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString()); |
||||
|
return handleResponse(response); |
||||
|
} catch (IOException | InterruptedException e) { |
||||
|
Thread.currentThread().interrupt(); |
||||
|
throw new RuntimeException("POST 请求失败: " + e.getMessage(), e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** 构建查询参数字符串 */ |
||||
|
private static String buildQueryString(Map<String, String> params) { |
||||
|
StringBuilder sb = new StringBuilder(); |
||||
|
for (Map.Entry<String, String> entry : params.entrySet()) { |
||||
|
sb.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8)) |
||||
|
.append("=") |
||||
|
.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8)) |
||||
|
.append("&"); |
||||
|
} |
||||
|
if (!params.isEmpty()) { |
||||
|
sb.deleteCharAt(sb.length() - 1); // 去掉最后一个 "&" |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
/** 统一处理响应 */ |
||||
|
private static String handleResponse(HttpResponse<String> response) { |
||||
|
int statusCode = response.statusCode(); |
||||
|
if (statusCode == 200) { |
||||
|
return response.body(); |
||||
|
} else { |
||||
|
throw new RuntimeException("HTTP 错误: " + statusCode + ", 响应: " + response.body()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue