|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- package com.ruoyi.system.utils;
-
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Component;
- import org.springframework.web.client.RestTemplate;
- import org.springframework.web.util.UriComponentsBuilder;
- import java.util.HashMap;
- import java.util.Map;
- //调用获取商品信息工具类
- @Component
- @Slf4j
- public class HttpUtil {
- private static String BASE_URL;
-
- @Value("${zs-operation.url}")
- public void setBaseUrl(String baseUrl) {
- HttpUtil.BASE_URL = baseUrl;
- }
-
- private static final RestTemplate restTemplate = new RestTemplate();
-
- /**
- * 发送GET请求并处理响应
- * @param url 请求URL
- * @param needScreenshot 是否需要截图
- * @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;
- }
- }
- }
|