请求重试
This commit is contained in:
@@ -15,7 +15,20 @@ import java.net.URL;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.client.LaxRedirectStrategy;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
@Slf4j
|
||||
public class HttpClientExample {
|
||||
|
||||
@@ -28,17 +41,247 @@ public class HttpClientExample {
|
||||
return "";
|
||||
}
|
||||
|
||||
public static List<Map<String, Object>> getPksHistoryList(){
|
||||
|
||||
|
||||
|
||||
|
||||
public static List<Map<String, Object>> getPksHistoryList() {
|
||||
List<Map<String, Object>> resultList = new ArrayList<>();
|
||||
String todayDate = DateUtils.getTodayDate();
|
||||
|
||||
// 最大重试次数
|
||||
int maxRetries = 10;
|
||||
// 重试延迟(毫秒)
|
||||
int retryDelay = 2000;
|
||||
|
||||
for (int retryCount = 0; retryCount < maxRetries; retryCount++) {
|
||||
System.out.println("=== 第 " + (retryCount + 1) + " 次尝试请求 ===");
|
||||
|
||||
CloseableHttpClient httpClient = null;
|
||||
CloseableHttpResponse response = null;
|
||||
|
||||
try {
|
||||
String url = "https://api.api168168.com/pks/getPksHistoryList.do?lotCode=10058&date=" + todayDate;
|
||||
System.out.println("请求URL: " + url);
|
||||
|
||||
// 创建自定义配置的HttpClient
|
||||
RequestConfig requestConfig = RequestConfig.custom()
|
||||
.setConnectTimeout(15000) // 15秒连接超时
|
||||
.setSocketTimeout(15000) // 15秒socket超时
|
||||
.setConnectionRequestTimeout(15000)
|
||||
.setCircularRedirectsAllowed(true)
|
||||
.setMaxRedirects(5)
|
||||
.build();
|
||||
|
||||
httpClient = HttpClients.custom()
|
||||
.setDefaultRequestConfig(requestConfig)
|
||||
.setRedirectStrategy(new LaxRedirectStrategy())
|
||||
.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
|
||||
.disableCookieManagement()
|
||||
.build();
|
||||
|
||||
HttpGet request = new HttpGet(url);
|
||||
|
||||
// 设置完整的请求头
|
||||
request.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
|
||||
request.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
|
||||
request.setHeader("Cache-Control", "no-cache");
|
||||
request.setHeader("Connection", "keep-alive");
|
||||
request.setHeader("Host", "api.api168168.com");
|
||||
request.setHeader("Origin", "https://xy678kjw.com");
|
||||
request.setHeader("Pragma", "no-cache");
|
||||
request.setHeader("Referer", "https://xy678kjw.com/");
|
||||
request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36");
|
||||
request.setHeader("X-Requested-With", "XMLHttpRequest");
|
||||
|
||||
// 设置Sec-Fetch相关头
|
||||
request.setHeader("Sec-Fetch-Dest", "empty");
|
||||
request.setHeader("Sec-Fetch-Mode", "cors");
|
||||
request.setHeader("Sec-Fetch-Site", "cross-site");
|
||||
|
||||
// 执行请求
|
||||
response = httpClient.execute(request);
|
||||
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
System.out.println("响应状态码: " + statusCode);
|
||||
|
||||
// 打印响应头用于调试
|
||||
System.out.println("响应头信息:");
|
||||
Arrays.stream(response.getAllHeaders())
|
||||
.limit(10)
|
||||
.forEach(header -> System.out.println(" " + header.getName() + ": " + header.getValue()));
|
||||
|
||||
if (statusCode == 200) {
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
String responseString = EntityUtils.toString(entity, "UTF-8");
|
||||
System.out.println("响应内容长度: " + responseString.length());
|
||||
|
||||
// 打印响应前200个字符用于调试
|
||||
if (responseString.length() > 0) {
|
||||
int previewLength = Math.min(200, responseString.length());
|
||||
System.out.println("响应预览: " + responseString.substring(0, previewLength));
|
||||
|
||||
// 检查响应是否有效
|
||||
if (responseString.contains("errorCode") || responseString.contains("result")) {
|
||||
// 解析JSON
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode rootNode = objectMapper.readTree(responseString);
|
||||
|
||||
// 检查是否成功
|
||||
int errorCode = rootNode.get("errorCode").asInt();
|
||||
if (errorCode == 0) {
|
||||
// 获取data数组
|
||||
JsonNode dataArray = rootNode
|
||||
.get("result")
|
||||
.get("data");
|
||||
|
||||
if (dataArray.isArray()) {
|
||||
for (JsonNode item : dataArray) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
|
||||
// 解析原始数据
|
||||
String preDrawCode = item.get("preDrawCode").asText();
|
||||
String[] codeArray = preDrawCode.split(",");
|
||||
|
||||
// 转换为List<Integer>
|
||||
List<Integer> resultNumbers = new ArrayList<>();
|
||||
for (String code : codeArray) {
|
||||
resultNumbers.add(Integer.parseInt(code.trim()));
|
||||
}
|
||||
|
||||
// 计算sum1和sum2
|
||||
int sum1 = 0;
|
||||
int sum2 = 0;
|
||||
for (int i = 0; i < 5; i++) {
|
||||
sum1 += resultNumbers.get(i);
|
||||
}
|
||||
for (int i = 5; i < 10; i++) {
|
||||
sum2 += resultNumbers.get(i);
|
||||
}
|
||||
|
||||
// 计算winner(前五个号码的和)
|
||||
int winner = sum1;
|
||||
|
||||
// 计算冠亚和(前两个号码的和)
|
||||
int championSum = resultNumbers.get(0) + resultNumbers.get(1);
|
||||
|
||||
// 判断冠亚单双
|
||||
String GD2 = (championSum % 2 == 0) ? "冠亚双" : "冠亚单";
|
||||
|
||||
// 判断冠亚大小(冠亚和11为大,11为小)
|
||||
String GD1 = (championSum > 11) ? "冠亚大" : "冠亚小";
|
||||
if (championSum == 11) {
|
||||
GD1 = "冠亚小"; // 特殊处理:和值为11算小
|
||||
}
|
||||
|
||||
// 计算龙虎(1-5位对比)
|
||||
List<String> GLH_result = new ArrayList<>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (resultNumbers.get(i) > resultNumbers.get(9 - i)) {
|
||||
GLH_result.add("龙");
|
||||
} else {
|
||||
GLH_result.add("虎");
|
||||
}
|
||||
}
|
||||
|
||||
// 构建转换后的map
|
||||
map.put("result", resultNumbers);
|
||||
map.put("sum1", sum1);
|
||||
map.put("sum2", sum2);
|
||||
map.put("winner", winner);
|
||||
map.put("GD2", GD2);
|
||||
map.put("GD1", GD1);
|
||||
map.put("GLH_result", GLH_result);
|
||||
map.put("id", String.valueOf(item.get("preDrawIssue").asLong()));
|
||||
map.put("time", item.get("preDrawTime").asText());
|
||||
|
||||
resultList.add(map);
|
||||
}
|
||||
System.out.println("成功获取 " + resultList.size() + " 条数据");
|
||||
|
||||
// 成功获取数据,关闭资源并返回
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
if (httpClient != null) {
|
||||
httpClient.close();
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
} else {
|
||||
System.err.println("API返回错误: errorCode=" + errorCode);
|
||||
}
|
||||
} else {
|
||||
System.err.println("响应内容格式不正确,不包含期望的字段");
|
||||
}
|
||||
} else {
|
||||
System.err.println("响应内容为空");
|
||||
}
|
||||
}
|
||||
} else if (statusCode == 403 || statusCode == 404 || statusCode == 500) {
|
||||
System.err.println("服务器返回错误状态码: " + statusCode);
|
||||
// 如果是这些状态码,可能不需要重试
|
||||
break;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("第 " + (retryCount + 1) + " 次请求异常: " +
|
||||
e.getClass().getSimpleName() + " - " + e.getMessage());
|
||||
|
||||
// 如果是连接重置错误,打印更多信息
|
||||
if (e.getMessage() != null && e.getMessage().contains("Connection reset")) {
|
||||
System.err.println("连接被服务器重置");
|
||||
}
|
||||
|
||||
// 最后一次重试仍然失败,打印完整堆栈
|
||||
if (retryCount == maxRetries - 1) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} finally {
|
||||
// 确保资源关闭
|
||||
try {
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
if (httpClient != null) {
|
||||
httpClient.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("关闭资源时出错: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 如果不是最后一次尝试,等待后重试
|
||||
if (retryCount < maxRetries - 1) {
|
||||
System.out.println("等待 " + (retryDelay/1000) + " 秒后重试...");
|
||||
try {
|
||||
Thread.sleep(retryDelay);
|
||||
} catch (InterruptedException ie) {
|
||||
Thread.currentThread().interrupt();
|
||||
System.err.println("重试等待被中断");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (resultList.isEmpty()) {
|
||||
System.err.println("经过 " + maxRetries + " 次重试后,仍未能获取数据");
|
||||
}
|
||||
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/*public static List<Map<String, Object>> getPksHistoryList(){
|
||||
List<Map<String, Object>> resultList = new ArrayList<>();
|
||||
String todayDate = DateUtils.getTodayDate();
|
||||
try {
|
||||
String url = "https://api.api168168.com/pks/getPksHistoryList.do?lotCode=10058&"+todayDate;
|
||||
|
||||
// 设置代理 127.0.0.1:7890
|
||||
// Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
|
||||
//Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
|
||||
|
||||
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
|
||||
// HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(proxy);
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setRequestProperty("Origin", "https://xy678kjw.com");
|
||||
conn.setRequestProperty("Referer", "https://xy678kjw.com/");
|
||||
@@ -134,7 +377,7 @@ public class HttpClientExample {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
/*public static List<Map<String, Object>> getPksHistoryList(){
|
||||
|
||||
Reference in New Issue
Block a user