|
|
@@ -1,11 +1,17 @@ |
|
|
|
package com.ruoyi.system.utils; |
|
|
|
|
|
|
|
import com.amazonaws.retry.v2.SimpleRetryPolicy; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.http.impl.client.HttpClientBuilder; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
import org.springframework.web.util.UriComponentsBuilder; |
|
|
|
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
//调用获取商品信息工具类 |
|
|
@@ -19,7 +25,7 @@ public class HttpUtil { |
|
|
|
HttpUtil.BASE_URL = baseUrl; |
|
|
|
} |
|
|
|
|
|
|
|
private static final RestTemplate restTemplate = new RestTemplate(); |
|
|
|
//private static final RestTemplate restTemplate = new RestTemplate(); |
|
|
|
|
|
|
|
/** |
|
|
|
* 发送GET请求并处理响应 |
|
|
@@ -28,25 +34,54 @@ public class HttpUtil { |
|
|
|
* @return 响应结果 |
|
|
|
*/ |
|
|
|
public static Map<String, Object> sendGetRequest(String url, boolean needScreenshot,String platform) { |
|
|
|
Long start = System.currentTimeMillis(); |
|
|
|
try { |
|
|
|
// 构建请求URL和参数 |
|
|
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(BASE_URL) |
|
|
|
.queryParam("url", url) |
|
|
|
.queryParam("needScreenshot", needScreenshot) |
|
|
|
.queryParam("platform", platform); |
|
|
|
|
|
|
|
// 发送GET请求 |
|
|
|
ResponseEntity<Map> response = restTemplate.getForEntity(builder.toUriString(), Map.class); |
|
|
|
Map body = response.getBody(); |
|
|
|
Long end = System.currentTimeMillis(); |
|
|
|
log.info("调用获取商品最新信息接口耗时=:"+(end-start)); |
|
|
|
return body; |
|
|
|
} catch (Exception e) { |
|
|
|
Map<String, Object> reMap = new HashMap<>(); |
|
|
|
reMap.put("success", false); |
|
|
|
reMap.put("message", "请求发生错误: " + e.getMessage()); |
|
|
|
return reMap; |
|
|
|
} |
|
|
|
return send(url,needScreenshot,platform,0); |
|
|
|
} |
|
|
|
|
|
|
|
public static Map<String, Object> send(String url, boolean needScreenshot,String platform,int count) { |
|
|
|
log.info("调用 第"+(count+1)+"次"); |
|
|
|
Long start = System.currentTimeMillis(); |
|
|
|
try { |
|
|
|
// 构建请求URL和参数 |
|
|
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(BASE_URL) |
|
|
|
.queryParam("url", url) |
|
|
|
.queryParam("needScreenshot", needScreenshot) |
|
|
|
.queryParam("platform", platform); |
|
|
|
|
|
|
|
// 发送GET请求 |
|
|
|
log.info("开始时间="+getdate()); |
|
|
|
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(HttpClientBuilder.create().build()); |
|
|
|
factory.setConnectTimeout(300000); // 连接超时,单位为毫秒 |
|
|
|
factory.setReadTimeout(300000); // 读取超时,单位为毫秒 |
|
|
|
RestTemplate restTemplate = new RestTemplate(factory); |
|
|
|
ResponseEntity<Map> response = restTemplate.getForEntity(builder.toUriString(), Map.class); |
|
|
|
Map body = response.getBody(); |
|
|
|
Long end = System.currentTimeMillis(); |
|
|
|
log.info("调用获取商品最新信息接口耗时=:"+(end-start)); |
|
|
|
return body; |
|
|
|
} catch (Exception e) { |
|
|
|
log.info("结束时间="+getdate()); |
|
|
|
/* if (count<1) { |
|
|
|
count++; |
|
|
|
send(url,needScreenshot,platform,count); |
|
|
|
}*/ |
|
|
|
Map<String, Object> reMap = new HashMap<>(); |
|
|
|
reMap.put("success", false); |
|
|
|
reMap.put("message", "请求发生错误: " + e.getMessage()); |
|
|
|
return reMap; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static String getdate() { |
|
|
|
// 获取当前时间 |
|
|
|
LocalDateTime now = LocalDateTime.now(); |
|
|
|
// 定义时间格式 |
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
|
|
|
// 格式化并输出 |
|
|
|
String formattedTime = now.format(formatter); |
|
|
|
return formattedTime; |
|
|
|
} |
|
|
|
|
|
|
|
} |