fix:工具类修复
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package com.xiang.xservice.basic.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CurlUtils {
|
||||
|
||||
public static String doCurl(String url, Map<String, String> headers, String requestBody, String requestMethod) throws IOException, InterruptedException {
|
||||
String curlCommand = buildCurlCommand(url, headers, requestBody, requestMethod);
|
||||
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", curlCommand);
|
||||
System.out.println(curlCommand);
|
||||
pb.redirectErrorStream(true);
|
||||
Process process = pb.start();
|
||||
// 打印输出
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
int exitCode = process.waitFor();
|
||||
System.out.println("Curl 执行完成,退出码: " + exitCode);
|
||||
return exitCode + "";
|
||||
}
|
||||
|
||||
private static String buildCurlCommand(String url, Map<String, String> headers, String requestBody, String requestMethod) {
|
||||
StringBuilder curl = new StringBuilder();
|
||||
curl.append("curl -X ");
|
||||
curl.append(requestMethod).append(" ");
|
||||
curl.append("'").append(url).append("' ");
|
||||
|
||||
// 添加 headers
|
||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||
curl.append("-H ")
|
||||
.append("'")
|
||||
.append(entry.getKey()).append(": ").append(entry.getValue())
|
||||
.append("' ");
|
||||
}
|
||||
|
||||
// 添加 body(需处理引号转义)
|
||||
if (requestBody != null && !requestBody.isEmpty()) {
|
||||
requestBody = requestBody.replace("'", "'\"'\"'"); // 处理单引号冲突
|
||||
curl.append("--data ")
|
||||
.append("'").append(requestBody).append("'");
|
||||
}
|
||||
|
||||
return curl.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
Map<String, String> headers = new HashMap<String, String>();
|
||||
headers.put("Host", "api.livelab.com.cn");
|
||||
headers.put("Connection", "keep-alive");
|
||||
headers.put("Content-Length", "316");
|
||||
headers.put("platform-type", "%E7%BA%B7%E7%8E%A9%E5%B2%9B%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F");
|
||||
headers.put("content-type", "application/json");
|
||||
headers.put("x-fwd-anonymousId", "ocXac5C25MY5O3UM_EfL0oTgm7Jw");
|
||||
headers.put("platform-version", "3.12.0");
|
||||
headers.put("Authorization", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJjdCI6MTc1MjYzODAwNTUxNywic3ViIjoiTDIxMTQzMjU2NDEiLCJhdWQiOiJMSVZFTEFCIiwiaXNzIjoiVElDS0VUIiwibWlkIjoxNzU4ODA4NTcxMzgzNjcxNTUzLCJ0eXBlIjoiYXBwbGV0IiwiaWF0IjoxNzUyNjM4MDA1LCJkaWQiOiI2RTRDMDQ5RS00RkFBLTQ0NDMtQjI3NC1DNjRBMjZEMUFGNTkiLCJrZXkiOiJMSVZFTEFCIn0.iw1u3LyCYlxYCI1gMwTeuJlPdv-rQKCVlO2ySF_UrJ-Bearer eyJhbGciOiJIUzUxMiJ9.eyJjdCI6MTc1Mzg4NTc5MDg3MSwic3ViIjoiTDIxMTQzMjU2NDEiLCJhdWQiOiJMSVZFTEFCIiwiaXNzIjoiVElDS0VUIiwibWlkIjoxNzU4ODA4NTcxMzgzNjcxNTUzLCJ0eXBlIjoiYXBwbGV0IiwiaWF0IjoxNzUzODg1NzkwLCJkaWQiOiI2RTRDMDQ5RS00RkFBLTQ0NDMtQjI3NC1DNjRBMjZEMUFGNTkiLCJrZXkiOiJMSVZFTEFCIn0.kZU8hyK1_ubuRnE8QpqzYydFER6Zi43uV-T8rbACS21f-YXote4-bURPDzq3K3H-GLdApyO-pPlZSQYXivOd1g");
|
||||
headers.put("Accept-Encoding", "gzip,compress,br,deflate");
|
||||
headers.put("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.61(0x18003d30) NetType/4G Language/zh_CN");
|
||||
headers.put("Referer", "https://servicewechat.com/wx5a8f481d967649eb/114/page-frame.html");
|
||||
String json = "{\"audienceCount\":4,\"blackBox\":\"0\",\"combineTicketVos\":null,\"contactName\":\"朱吉祥\",\"contactPhone\":\"15858717571\",\"deliveryType\":1,\"frequentIds\":[50438548,35320661,50436621,50442739],\"ordinaryTicketVos\":null,\"payment\":3520.00,\"performId\":2395,\"privilegeCodeList\":[],\"projectId\":\"8763381795\",\"seatPlanIds\":[12091],\"totalPrice\":3520.00}";
|
||||
String post = CurlUtils.doCurl("https://api.livelab.com.cn/order/app/center/v3/create", headers, json, "POST");
|
||||
System.out.println(post);
|
||||
}
|
||||
}
|
||||
@@ -28,8 +28,8 @@ import java.util.concurrent.TimeUnit;
|
||||
public class HttpUtils {
|
||||
|
||||
private static final int socketTimeOut = 5000;
|
||||
private static final int connectTimeout = 5000;
|
||||
private static final int connectionRequestTimeout = 5000;
|
||||
private static final int connectTimeout = 10000;
|
||||
private static final int connectionRequestTimeout = 3000;
|
||||
private static final int defaultMaxPerRoute = 20;
|
||||
private static final int maxTotal = 100;
|
||||
|
||||
@@ -118,21 +118,24 @@ public class HttpUtils {
|
||||
CloseableHttpResponse response = null;
|
||||
String result = "";
|
||||
try {
|
||||
StringBuilder requestUrl = new StringBuilder(url);
|
||||
String request = "";
|
||||
if (MapUtils.isNotEmpty(param)) {
|
||||
requestUrl.append("?");
|
||||
param.forEach((k, v) -> requestUrl.append(k).append("=").append(v).append("&"));
|
||||
// 去掉最后一个 &
|
||||
requestUrl.setLength(requestUrl.length() - 1);
|
||||
StringBuilder req = new StringBuilder("?");
|
||||
for (Map.Entry<String, String> entry : param.entrySet()) {
|
||||
req.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
|
||||
}
|
||||
request = req.substring(0, req.length() - 1);
|
||||
}
|
||||
|
||||
HttpGet httpGet = new HttpGet(requestUrl.toString());
|
||||
HttpGet httpGet = new HttpGet(url + request);
|
||||
httpGet.addHeader("Content-Type", "application/json");
|
||||
httpGet.setHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.61(0x18003d2e) NetType/WIFI Language/zh_CN");
|
||||
if (MapUtils.isNotEmpty(header)) {
|
||||
header.forEach(httpGet::setHeader);
|
||||
for (Map.Entry<String, String> entry : header.entrySet()) {
|
||||
httpGet.setHeader(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
log.info("doGet请求:请求地址:{}", requestUrl);
|
||||
log.info("doGet请求:请求地址:{}", url + request);
|
||||
response = httpClient.execute(httpGet);
|
||||
result = EntityUtils.toString(response.getEntity(), "utf-8");
|
||||
} catch (Exception e) {
|
||||
|
||||
Reference in New Issue
Block a user