本文整理匯總了Java中org.apache.http.impl.client.HttpClientBuilder.setDefaultHeaders方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpClientBuilder.setDefaultHeaders方法的具體用法?Java HttpClientBuilder.setDefaultHeaders怎麽用?Java HttpClientBuilder.setDefaultHeaders使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.impl.client.HttpClientBuilder
的用法示例。
在下文中一共展示了HttpClientBuilder.setDefaultHeaders方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: makeHttpClient
import org.apache.http.impl.client.HttpClientBuilder; //導入方法依賴的package包/類
/**
* Creates new HttpClient for interacting with webmate API.
*
* @param authInfo authentication information (email address + token)
* @param environment API endpoint address
* @return new HttpClient to be used by Service clients.
*/
private static HttpClient makeHttpClient(WebmateAuthInfo authInfo, WebmateEnvironment environment, HttpClientBuilder httpClientBuilder) {
httpClientBuilder.setUserAgent(WEBMATE_JAVASDK_USERAGENT);
List<Header> headers = new ArrayList<>();
headers.add(new BasicHeader(WEBMATE_USER_HEADERKEY, authInfo.emailAddress));
headers.add(new BasicHeader(WEBMATE_APITOKEN_HEADERKEY, authInfo.apiKey));
headers.add(new BasicHeader("Content-Type", "application/json"));
httpClientBuilder.setDefaultHeaders(headers);
return httpClientBuilder.build();
}
示例2: setCloudAuthHeaderIfConfigured
import org.apache.http.impl.client.HttpClientBuilder; //導入方法依賴的package包/類
protected void setCloudAuthHeaderIfConfigured(HttpClientBuilder clientBuilder) {
if (!Strings.isNullOrEmpty(cloudTokenFetchUrl) && !Strings.isNullOrEmpty(cloudAPIKey)) {
Token cloudToken = getCloudToken();
String cloudTokenString = cloudToken.getAccess_token();
if (!Strings.isNullOrEmpty(cloudTokenString)) {
Header header = new BasicHeader("Authorization", cloudTokenString);
clientBuilder.setDefaultHeaders(ImmutableSet.of(header));
}
}
}
示例3: initRestTemplate
import org.apache.http.impl.client.HttpClientBuilder; //導入方法依賴的package包/類
/**
* springboot初始化restTemplate
*/
@Bean
public RestTemplate initRestTemplate() {
// 長連接保持30秒
PoolingHttpClientConnectionManager pollingConnectionManager = new PoolingHttpClientConnectionManager(30, TimeUnit.SECONDS);
// 總連接數
pollingConnectionManager.setMaxTotal(1000);
// 同路由的並發數
pollingConnectionManager.setDefaultMaxPerRoute(1000);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setConnectionManager(pollingConnectionManager);
// 重試次數,默認是3次,沒有開啟
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(2, true));
// 保持長連接配置,需要在頭添加Keep-Alive
httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
RequestConfig.Builder builder = RequestConfig.custom();
builder.setConnectionRequestTimeout(200);
builder.setConnectTimeout(5000);
builder.setSocketTimeout(5000);
RequestConfig requestConfig = builder.build();
httpClientBuilder.setDefaultRequestConfig(requestConfig);
List<Header> headers = new ArrayList<>();
headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36"));
headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate"));
headers.add(new BasicHeader("Accept-Language", "zh-CN"));
headers.add(new BasicHeader("Connection", "Keep-Alive"));
httpClientBuilder.setDefaultHeaders(headers);
HttpClient httpClient = httpClientBuilder.build();
// httpClient連接配置,底層是配置RequestConfig
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
// 連接超時
clientHttpRequestFactory.setConnectTimeout(5000);
// 數據讀取超時時間,即SocketTimeout
clientHttpRequestFactory.setReadTimeout(5000);
// 連接不夠用的等待時間,不宜過長,必須設置,比如連接不夠用時,時間過長將是災難性的
clientHttpRequestFactory.setConnectionRequestTimeout(200);
// 緩衝請求數據,默認值是true。通過POST或者PUT大量發送數據時,建議將此屬性更改為false,以免耗盡內存。
clientHttpRequestFactory.setBufferRequestBody(false);
// 添加內容轉換器
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(initStringHttpMessageConverter());
messageConverters.add(new FormHttpMessageConverter());
messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
messageConverters.add(new MappingJackson2HttpMessageConverter());
RestTemplate restTemplate = new RestTemplate(messageConverters);
restTemplate.setRequestFactory(clientHttpRequestFactory);
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
return restTemplate;
}
示例4: HttpClientTemplate
import org.apache.http.impl.client.HttpClientBuilder; //導入方法依賴的package包/類
private HttpClientTemplate() {
// 長連接保持30秒
PoolingHttpClientConnectionManager pollingConnectionManager = new PoolingHttpClientConnectionManager(timeToLive, TimeUnit.SECONDS);
// 總連接數
pollingConnectionManager.setMaxTotal(maxTotal);
// 同路由的並發數
pollingConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setConnectionManager(pollingConnectionManager);
// 重試次數,默認是3次,沒有開啟
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(retryCount, requestSentRetryEnabled));
// 保持長連接配置,需要在頭添加Keep-Alive
httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
List<Header> headers = new ArrayList<>();
// 參考:http://www.cnblogs.com/sink_cup/archive/2011/03/15/http_user_agent.html
// headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36"));
// 不需要設置,可能會亂碼,請參考:http://www.crifan.com/set_accept_encoding_header_to_gzip_deflate_return_messy_code/
// headers.add(new BasicHeader("Accept-Encoding", "gzip,deflate"));
// headers.add(new BasicHeader("Accept-Language", "zh-CN"));
headers.add(new BasicHeader("Connection", "Keep-Alive"));
httpClientBuilder.setDefaultHeaders(headers);
HttpClient httpClient = httpClientBuilder.build();
// httpClient連接配置,底層是配置RequestConfig
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
// 連接超時
clientHttpRequestFactory.setConnectTimeout(connectTimeout);
// 數據讀取超時時間,即SocketTimeout
clientHttpRequestFactory.setReadTimeout(readTimeout);
// 連接不夠用的等待時間,不宜過長,必須設置,比如連接不夠用時,時間過長將是災難性的
clientHttpRequestFactory.setConnectionRequestTimeout(connectionRequestTimeout);
// 緩衝請求數據,默認值是true。通過POST或者PUT大量發送數據時,建議將此屬性更改為false,以免耗盡內存。
if (!bufferRequestBody) {
clientHttpRequestFactory.setBufferRequestBody(false);
}
// 添加內容轉換器
// List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new FastJsonHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter(Charset.forName(charset)));
messageConverters.add(new ByteArrayHttpMessageConverter());
super.setMessageConverters(messageConverters);
super.setRequestFactory(clientHttpRequestFactory);
super.setErrorHandler(new DefaultResponseErrorHandler());
}