本文整理匯總了Java中org.apache.http.impl.client.HttpClientBuilder.setDefaultCookieStore方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpClientBuilder.setDefaultCookieStore方法的具體用法?Java HttpClientBuilder.setDefaultCookieStore怎麽用?Java HttpClientBuilder.setDefaultCookieStore使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.impl.client.HttpClientBuilder
的用法示例。
在下文中一共展示了HttpClientBuilder.setDefaultCookieStore方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createHttpClientBuilder
import org.apache.http.impl.client.HttpClientBuilder; //導入方法依賴的package包/類
protected HttpClientBuilder createHttpClientBuilder(SiteConfig siteConfig) {
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setConnectionTimeToLive(siteConfig.getConnTimeToLiveMillis(), TimeUnit.MILLISECONDS);
httpClientBuilder.setMaxConnPerRoute(siteConfig.getMaxConnPerRoute());
httpClientBuilder.setMaxConnTotal(siteConfig.getMaxConnTotal());
httpClientBuilder.setUserAgent(siteConfig.getUserAgent());
httpClientBuilder.setRetryHandler(createHttpRequestRetryHandler());
httpClientBuilder.setRedirectStrategy(createRedirectStrategy());
httpClientBuilder.setSSLContext(createSSLContext());
httpClientBuilder.setSSLHostnameVerifier(createSSLHostnameVerifier());
httpClientBuilder.setDefaultConnectionConfig(createConnectionConfig(siteConfig));
httpClientBuilder.setDefaultSocketConfig(createSocketConfig(siteConfig));
httpClientBuilder.setDefaultCookieSpecRegistry(createCookieSpecRegistry());
httpClientBuilder.setDefaultCookieStore(createCookieStore());
return httpClientBuilder;
}
示例2: createHttpClient
import org.apache.http.impl.client.HttpClientBuilder; //導入方法依賴的package包/類
/**
* 獲取Http客戶端連接對象
* @param timeOut 超時時間
* @param proxy 代理
* @param cookie Cookie
* @return Http客戶端連接對象
*/
public CloseableHttpClient createHttpClient(int timeOut,HttpHost proxy,BasicClientCookie cookie) {
// 創建Http請求配置參數
RequestConfig.Builder builder = RequestConfig.custom()
// 獲取連接超時時間
.setConnectionRequestTimeout(timeOut)
// 請求超時時間
.setConnectTimeout(timeOut)
// 響應超時時間
.setSocketTimeout(timeOut)
.setCookieSpec(CookieSpecs.STANDARD);
if (proxy!=null) {
builder.setProxy(proxy);
}
RequestConfig requestConfig = builder.build();
// 創建httpClient
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder
// 把請求相關的超時信息設置到連接客戶端
.setDefaultRequestConfig(requestConfig)
// 把請求重試設置到連接客戶端
.setRetryHandler(new RetryHandler())
// 配置連接池管理對象
.setConnectionManager(connManager);
if (cookie!=null) {
CookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(cookie);
httpClientBuilder.setDefaultCookieStore(cookieStore);
}
return httpClientBuilder.build();
}
示例3: createHttpClient
import org.apache.http.impl.client.HttpClientBuilder; //導入方法依賴的package包/類
/**
* 獲取Http客戶端連接對象
* @param timeOut 超時時間
* @param proxy 代理
* @param cookie Cookie
* @return Http客戶端連接對象
*/
private CloseableHttpClient createHttpClient(int timeOut,HttpHost proxy,BasicClientCookie cookie) {
// 創建Http請求配置參數
RequestConfig.Builder builder = RequestConfig.custom()
// 獲取連接超時時間
.setConnectionRequestTimeout(timeOut)
// 請求超時時間
.setConnectTimeout(timeOut)
// 響應超時時間
.setSocketTimeout(timeOut)
.setCookieSpec(CookieSpecs.STANDARD);
if (proxy!=null) {
builder.setProxy(proxy);
}
RequestConfig requestConfig = builder.build();
// 創建httpClient
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder
// 把請求相關的超時信息設置到連接客戶端
.setDefaultRequestConfig(requestConfig)
// 把請求重試設置到連接客戶端
.setRetryHandler(new RetryHandler())
// 配置連接池管理對象
.setConnectionManager(connManager);
if (cookie!=null) {
CookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(cookie);
httpClientBuilder.setDefaultCookieStore(cookieStore);
}
return httpClientBuilder.build();
}
示例4: build
import org.apache.http.impl.client.HttpClientBuilder; //導入方法依賴的package包/類
private CloseableHttpClient build(final Consumer<HttpClientBuilder> customizeBuilder) throws IOException {
LOGGER.debug("HTTP {} {}", request, url);
if (httpClient != null) {
LOGGER.debug("Existing HttpClient re-used");
return httpClient;
}
if (sslsf == null) {
sslsf = createSSLConnectionSocketFactory(null, null, false);
}
HttpClientBuilder builder = HttpClients.custom()
.setRetryHandler(new StandardHttpRequestRetryHandler(3, true) {
@Override
public boolean retryRequest(IOException exception,
int executionCount, HttpContext context) {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
}
return super.retryRequest(exception, executionCount,
context);
}
}).setSSLSocketFactory(sslsf);
if (username != null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(url.getHost(), url.getPort() == -1 ? url.getDefaultPort() : url.getPort()),
new UsernamePasswordCredentials(username, password));
builder.setDefaultCredentialsProvider(credsProvider);
}
if (disableRedirect) {
builder.disableRedirectHandling();
}
addPreemptiveAuthorizationHeaders();
if (customizeBuilder != null) {
customizeBuilder.accept(builder);
}
if (!cookies.isEmpty()) {
final CookieStore cookieStore = new BasicCookieStore();
cookies.entrySet().stream().map(x -> new BasicClientCookie(x.getKey(), x.getValue())).forEach(cookieStore::addCookie);
builder.setDefaultCookieStore(cookieStore);
}
return builder.build();
}