I'm using the fluent API of HttpClient to make a GET request:
String jsonResult = Request.Get(requestUrl)
.connectTimeout(2000)
.socketTimeout(2000)
.execute().returnContent().asString();
But for each request I get the following warning:
apr 07, 2016 12:26:46 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Invalid cookie header: "Set-Cookie: WMF-Last-Access=07-Apr-2016;Path=/;HttpOnly;Expires=Mon, 09 May 2016 00:00:00 GMT". Invalid 'expires' attribute: Mon, 09 May 2016 00:00:00 GMT
How can I fix this and keep using the fluent interface? Ideally I'd want a proper way to fix it, but since I don't really care about the cookies in my use case any solution that just allows me to stop displaying the warnings (besides redirecting stderr, cause I need that) is welcome.
答案 0 :(得分:64)
默认的HttpClient很难理解最新的RFC兼容标头。
不要隐藏警告,只需切换到这样的标准cookie规范(HttpClient 4.4 +):
HttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD).build())
.build();
答案 1 :(得分:4)
如果您想使用HttpClientBuilder
,可以使用以下sytax:
HttpClient httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD).build()).build();
答案 2 :(得分:3)
由于开发人员不想考虑对象模型,因此可以将HttpClient封装为RestTemplate的方式如下(如上面提到的@comiventor,特别是对于Spring Boot开发人员而言)。
public class RestTemplateStandardCookieCustomizer
implements RestTemplateCustomizer {
@Override
public void customize(final RestTemplate restTemplate) {
final HttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD).build())
.build();
restTemplate.setRequestFactory(
new HttpComponentsClientHttpRequestFactory(httpClient)
);
}
}
var restTemplate = restTemplateBuilder.additionalCustomizers(
new RestTemplateStandardCookieCustomizer()
).build();
答案 3 :(得分:2)
解决:
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.client.protocol.ResponseProcessCookies", "fatal");