main
周文涛 2 years ago
parent 94096f1c48
commit 33a014825f

@ -0,0 +1,21 @@
package com.example.zxweb.utils;
import java.util.HashMap;
import java.util.Map;
/**
* @Description
* @Author ZhouWenTao
* @Date 2023/9/8 18:11
*/
public class CacheManager {
private static Map<String, Object> cache = new HashMap<>();
public static void put(String key, Object value) {
cache.put(key, value);
}
public static Object get(String key) {
return cache.get(key);
}
}

@ -10,6 +10,7 @@ import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
@ -195,6 +196,11 @@ public class RestUtil {
headers.add(s,header.getString(s));
}
}
String cookie =(String) CacheManager.get("Cookie");
if (StringUtils.isNotBlank(cookie)) {
headers.add("Cookie",cookie);
}
return request(url, method, headers, variables, params, JSONObject.class);
}
@ -254,6 +260,18 @@ public class RestUtil {
RT = new RestTemplate(requestFactory);
// 解决乱码问题
RT.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
ResponseEntity<T> exchange = RT.exchange(url, method, request, responseType);
HttpHeaders responseBodyHeaders = exchange.getHeaders();
List<String> stringList = responseBodyHeaders.get("Set-Cookie");
StringBuilder sb=new StringBuilder();
for (String s : stringList) {
if (s.contains("JSESSIONID") &&!s.contains("deleteMe")) {
sb.append(s);
}
}
if (StringUtils.isNotBlank(sb.toString())) {
CacheManager.put("Cookie",sb.toString());
}
return RT.exchange(url, method, request, responseType);
}

Loading…
Cancel
Save