Java在HttpClient關閉cookie支援
通常在API Server不需要使用cookie,這篇介紹如何在Java的HttpClient關閉cookie支援。
在initial將cookie資源管理disable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| //API連線不需要使用cookie,因此關閉cookie管理,降低系統資源耗用。 //主要是.disableCookieManagement() 和 .disableAutomaticRetries() 來關閉cookie管理。
private CloseableHttpClient httpClient = null;
protected RequestConfig requestConfig = null;
requestConfig = RequestConfig.custom().setSocketTimeout(SOCKET_HOLD_TIMEOUT) .setConnectTimeout(CONNECTION_TIMEOUT).setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT) .build(); httpClient = HttpClients.custom().setConnectionManager(cm).setKeepAliveStrategy(keepAliveStrategy) .setDefaultRequestConfig(requestConfig).disableCookieManagement() .setUserAgent( "mozilla/4.0 (compatible; msie 8.0; windows nt 6.1; wow64; trident/4.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; media center pc 6.0; masn)") .disableAutomaticRetries().build();
|