服务器后台warning:
2024-04-25 10:50:25.348[WARN][MyParse3][ResponseProcessCookies:130]-Invalid cookie header: "Set-Cookie: NE_MMAD_UID=6mO6ii31r3hFAa1BCT37xeTDvdQuGuE0vfNCNtswE5QOTXIZafaCC; Max-Age=946080000; Path=/; Expires=Sat, 18 Apr 2054 02:50:25 GMT". Invalid 'expires' attribute: Sat, 18 Apr 2054 02:50:25 GMT、
分析:
Sat, 18 Apr 2054 02:50:25 GMT、日期解析失败,使用标准的cookie解析方式
解决:
RequestConfig.custom().setConnectionRequestTimeout(connectRequestTimeout) .setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout) .setCookieSpec(CookieSpecs.STANDARD).build();
java
@Bean
public RestTemplate restTemplate(@Autowired CloseableHttpClient closeableHttpClient) {
return new RestTemplate(new HttpComponentsClientHttpRequestFactory(closeableHttpClient));
}
@Bean
public CloseableHttpClient closeableHttpClient() {
return builder(connectTimeout, connectRequestTimeout, readTimeout);
}
private CloseableHttpClient builder(int connectTimeout, int connectRequestTimeout, int socketTimeout) {
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectRequestTimeout)
.setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout)
.setCookieSpec(CookieSpecs.STANDARD).build();
return HttpClients.custom().setConnectionManager(get()).setDefaultRequestConfig(requestConfig)
.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
long time = super.getKeepAliveDuration(response, context);
if (time >= 0) {
return time;
}
return keepAliveTime;
}
}).build();
}
private PoolingHttpClientConnectionManager get() {
if (gcm != null) {
return gcm;
}
synchronized (RestConfig.class) {
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry);
manager.setMaxTotal(maxTotal);
manager.setDefaultMaxPerRoute(maxPerRoute);
idleThread = new IdleConnectionMonitorThread(manager);
idleThread.start();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
idleThread.shutdown();
}
});
gcm = manager;
}
return gcm;
}
// 监控有异常的链接
private class IdleConnectionMonitorThread extends Thread {
private final HttpClientConnectionManager connMgr;
private volatile boolean exitFlag = false;
public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {
this.connMgr = connMgr;
setDaemon(true);
}
@Override
public void run() {
while (!exitFlag) {
synchronized (this) {
try {
wait(2000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
// 关闭失效的连接
connMgr.closeExpiredConnections();
// 可选的, 关闭指定时间内不活动的连接
connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
}
}
public void shutdown() {
exitFlag = true;
}
}
参考:
Spring Boot 无效的cookie标头:无效的"expires"属性 _大数据知识库
Invalid cookie header: "Set-Cookie: xxx", HttpOnly". Invalid 'expires' attribute报错说明-CSDN博客