本文整理汇总了Java中org.apache.http.impl.client.HttpClientBuilder.setDefaultRequestConfig方法的典型用法代码示例。如果您正苦于以下问题:Java HttpClientBuilder.setDefaultRequestConfig方法的具体用法?Java HttpClientBuilder.setDefaultRequestConfig怎么用?Java HttpClientBuilder.setDefaultRequestConfig使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.impl.client.HttpClientBuilder
的用法示例。
在下文中一共展示了HttpClientBuilder.setDefaultRequestConfig方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initImpl
import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
private synchronized void initImpl() {
if (impl == null) {
HttpClientBuilder builder = createBuilder();
if (receiveTimeout != null) {
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder = requestBuilder.setSocketTimeout(receiveTimeout);
builder.setDefaultRequestConfig(requestBuilder.build());
}
for (HttpClientAdapterListener listener : listeners) {
listener.onBuild(builder);
}
impl = builder.build();
}
}
示例2: usingHttpComponents
import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
static ClientHttpRequestFactory usingHttpComponents(ClientOptions options)
throws GeneralSecurityException, IOException {
HttpClientBuilder httpClientBuilder = HttpClients.custom()
.setSSLContext(SSLContext.getDefault())
.useSystemProperties();
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
.setAuthenticationEnabled(true);
if (options.getConnectionTimeout() != null) {
requestConfigBuilder.setConnectTimeout(options.getConnectionTimeout());
}
if (options.getReadTimeout() != null) {
requestConfigBuilder.setSocketTimeout(options.getReadTimeout());
}
httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
return new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build());
}
示例3: getHttpClient
import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
public HttpClient getHttpClient() {
if (httpClient == null) {
synchronized (this) {
if (httpClient == null) {
if (pool == null) { //初始化pool
try {
afterPropertiesSet();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setConnectionManager(pool);
httpClientBuilder.setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(conntimeout).setSocketTimeout(sotimeout).build());
httpClientBuilder.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
try {
return Long.parseLong(value) * 1000;
} catch (NumberFormatException ignore) {
}
}
}
// 否则保持活动5秒
return 5 * 1000;
}
});
httpClient = httpClientBuilder.build();
}
}
}
return httpClient;
}
示例4: createHttpClient
import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
private HttpClient createHttpClient(Authentication auth, String verify, HttpHost target, Boolean postRedirects,
String password, TrustStrategy keystoreTrustStrategy, HostnameVerifier keystoreHostnameVerifier,
Proxy proxy) {
Certificate certificate = new Certificate();
Auth authHelper = new Auth();
HttpClientBuilder httpClientBuilder = WinHttpClients.custom();
Builder requestConfig = RequestConfig.custom();
requestConfig.setCookieSpec(CookieSpecs.DEFAULT);
logger.debug("Verify value: " + verify);
logger.debug((new File(verify).getAbsolutePath()));
if (new File(verify).exists()) {
logger.debug("Loading custom keystore");
httpClientBuilder.setSSLSocketFactory(
certificate.allowAllCertificates(certificate.createCustomKeyStore(verify.toString(), password),
password, keystoreTrustStrategy, keystoreHostnameVerifier));
} else if (!Boolean.parseBoolean(verify.toString())) {
logger.debug("Allowing all certificates");
httpClientBuilder.setSSLSocketFactory(certificate.allowAllCertificates(null));
}
if (auth.isAuthenticable()) {
httpClientBuilder.setDefaultCredentialsProvider(authHelper.getCredentialsProvider(auth, target));
}
if (proxy != null && proxy.isInUse()) {
logger.debug("Enabling proxy");
if (proxy.isAuthenticable()) {
logger.debug("Setting proxy credentials");
httpClientBuilder.setDefaultCredentialsProvider(
authHelper.getCredentialsProvider(proxy.getAuth(), proxy.getHttpHost()));
}
requestConfig.setProxy(proxy.getHttpHost());
}
if (postRedirects) {
httpClientBuilder.setRedirectStrategy(new CustomRedirectStrategy());
}
httpClientBuilder.setDefaultRequestConfig(requestConfig.build());
return httpClientBuilder.build();
}
示例5: getHttpClient
import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
private static CloseableHttpClient getHttpClient() {
HttpClientBuilder hb = HttpClientBuilder.create();
RequestConfig config = RequestConfig.custom().setConnectTimeout(5000)
.setConnectionRequestTimeout((int) TimeUnit.HOURS.toMillis(1))
.setSocketTimeout((int) TimeUnit.HOURS.toMillis(1)).setExpectContinueEnabled(false).build();
hb.setDefaultRequestConfig(config);
CloseableHttpClient hc = hb.build();
return hc;
}
示例6: 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;
}
示例7: usingHttpComponents
import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
static ClientHttpRequestFactory usingHttpComponents(ClientOptions options,
SslConfiguration sslConfiguration) throws GeneralSecurityException,
IOException {
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(
DefaultSchemePortResolver.INSTANCE, ProxySelector.getDefault()));
if (hasSslConfiguration(sslConfiguration)) {
SSLContext sslContext = getSSLContext(sslConfiguration);
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
sslContext);
httpClientBuilder.setSSLSocketFactory(sslSocketFactory);
httpClientBuilder.setSslcontext(sslContext);
}
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(options.getConnectionTimeout())
.setSocketTimeout(options.getReadTimeout())
.setAuthenticationEnabled(true)
.build();
httpClientBuilder.setDefaultRequestConfig(requestConfig);
// Support redirects
httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy());
// Fix weird problem `ProtocolException: Content-Length header already present` from `org.apache.http.protocol.RequestContent`
httpClientBuilder.addInterceptorFirst(new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
if (request instanceof HttpEntityEnclosingRequest) {
request.removeHeaders(HTTP.TRANSFER_ENCODING);
request.removeHeaders(HTTP.CONTENT_LEN);
}
}
});
return new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build());
}
开发者ID:JetBrains,项目名称:teamcity-hashicorp-vault-plugin,代码行数:43,代码来源:ClientHttpRequestFactoryFactory.java
示例8: initialzeInternalClient
import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
protected void initialzeInternalClient() {
if (!needsInternalClientInialization) {
// internal client is already initialized
return;
}
// release any resources if this client was already used
close();
// rebuild the client
HttpClientBuilder httpClientBuilder = HttpClients.custom();
// Add this interceptor to get the values of all HTTP headers in the request.
// Some of them are provided by the user while others are generated by Apache HTTP Components.
httpClientBuilder.addInterceptorLast(new HttpRequestInterceptor() {
@Override
public void process( HttpRequest request, HttpContext context ) throws HttpException,
IOException {
Header[] requestHeaders = request.getAllHeaders();
actualRequestHeaders = new ArrayList<HttpHeader>();
for (Header header : requestHeaders) {
addHeaderToList(actualRequestHeaders, header.getName(), header.getValue());
}
if (debugLevel != HttpDebugLevel.NONE) {
logHTTPRequest(requestHeaders, request);
}
}
});
// connect and read timeouts
httpClientBuilder.setDefaultRequestConfig(RequestConfig.custom()
.setConnectTimeout(connectTimeoutSeconds
* 1000)
.setSocketTimeout(readTimeoutSeconds
* 1000)
.build());
// socket buffer size
if (this.socketBufferSize > 0) {
httpClientBuilder.setDefaultSocketConfig(SocketConfig.custom()
.setRcvBufSize(this.socketBufferSize)
.setSndBufSize(this.socketBufferSize)
.build());
}
// SSL
if (isOverSsl) {
setupSSL(httpClientBuilder);
}
// setup authentication
if (!StringUtils.isNullOrEmpty(username)) {
setupAuthentication(httpClientBuilder);
}
// set proxy
if (AtsSystemProperties.SYSTEM_HTTP_PROXY_HOST != null
&& AtsSystemProperties.SYSTEM_HTTP_PROXY_PORT != null) {
HttpHost proxy = new HttpHost(AtsSystemProperties.SYSTEM_HTTP_PROXY_HOST,
Integer.parseInt(AtsSystemProperties.SYSTEM_HTTP_PROXY_PORT));
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
httpClientBuilder.setRoutePlanner(routePlanner);
}
// now build the client after we have already set everything needed on the client builder
httpClient = httpClientBuilder.build();
// do not come here again until not needed
needsInternalClientInialization = false;
}
示例9: build
import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
/**
* @param listener Log listener
* @param prompt Prompt for proxy credentials
* @return Builder for HTTP client
*/
public HttpClientBuilder build(final TranscriptListener listener, final LoginCallback prompt) {
final HttpClientBuilder configuration = HttpClients.custom();
// Use HTTP Connect proxy implementation provided here instead of
// relying on internal proxy support in socket factory
final Proxy proxy = proxyFinder.find(host);
switch(proxy.getType()) {
case HTTP:
case HTTPS:
final HttpHost h = new HttpHost(proxy.getHostname(), proxy.getPort(), StringUtils.lowerCase(proxy.getType().name()));
if(log.isInfoEnabled()) {
log.info(String.format("Setup proxy %s", h));
}
configuration.setProxy(h);
configuration.setProxyAuthenticationStrategy(new CallbackProxyAuthenticationStrategy(ProxyCredentialsStoreFactory.get(), host, prompt));
break;
}
configuration.setUserAgent(new PreferencesUseragentProvider().get());
final int timeout = preferences.getInteger("connection.timeout.seconds") * 1000;
configuration.setDefaultSocketConfig(SocketConfig.custom()
.setTcpNoDelay(true)
.setSoTimeout(timeout)
.build());
configuration.setDefaultRequestConfig(this.createRequestConfig(timeout));
final String encoding;
if(null == host.getEncoding()) {
encoding = preferences.getProperty("browser.charset.encoding");
}
else {
encoding = host.getEncoding();
}
configuration.setDefaultConnectionConfig(ConnectionConfig.custom()
.setBufferSize(preferences.getInteger("http.socket.buffer"))
.setCharset(Charset.forName(encoding))
.build());
if(preferences.getBoolean("http.connections.reuse")) {
configuration.setConnectionReuseStrategy(new DefaultClientConnectionReuseStrategy());
}
else {
configuration.setConnectionReuseStrategy(new NoConnectionReuseStrategy());
}
configuration.setRetryHandler(new ExtendedHttpRequestRetryHandler(preferences.getInteger("http.connections.retry")));
configuration.setServiceUnavailableRetryStrategy(new DisabledServiceUnavailableRetryStrategy());
if(!preferences.getBoolean("http.compression.enable")) {
configuration.disableContentCompression();
}
configuration.setRequestExecutor(new LoggingHttpRequestExecutor(listener));
// Always register HTTP for possible use with proxy. Contains a number of protocol properties such as the
// default port and the socket factory to be used to create the java.net.Socket instances for the given protocol
configuration.setConnectionManager(this.createConnectionManager(this.createRegistry()));
configuration.setDefaultAuthSchemeRegistry(RegistryBuilder.<AuthSchemeProvider>create()
.register(AuthSchemes.BASIC, new BasicSchemeFactory(
Charset.forName(preferences.getProperty("http.credentials.charset"))))
.register(AuthSchemes.DIGEST, new DigestSchemeFactory(
Charset.forName(preferences.getProperty("http.credentials.charset"))))
.register(AuthSchemes.NTLM, new NTLMSchemeFactory())
.register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory())
.register(AuthSchemes.KERBEROS, new KerberosSchemeFactory()).build());
return configuration;
}
示例10: init
import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
/**
* Initialization method. This takes in the configuration and sets up the underlying
* http client appropriately.
* @param configuration The user defined configuration.
*/
@Override
public void init(final Configuration configuration) {
// Save reference to configuration
this.configuration = configuration;
// Create default SSLContext
final SSLContext sslcontext = SSLContexts.createDefault();
// Allow TLSv1 protocol only
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier()
);
// Setup client builder
final HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder
// Pardot disconnects requests after 120 seconds.
.setConnectionTimeToLive(130, TimeUnit.SECONDS)
.setSSLSocketFactory(sslsf);
// Define our RequestConfigBuilder
final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
// If we have a configured proxy host
if (configuration.getProxyHost() != null) {
// Define proxy host
final HttpHost proxyHost = new HttpHost(
configuration.getProxyHost(),
configuration.getProxyPort(),
configuration.getProxyScheme()
);
// If we have proxy auth enabled
if (configuration.getProxyUsername() != null) {
// Create credential provider
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(configuration.getProxyHost(), configuration.getProxyPort()),
new UsernamePasswordCredentials(configuration.getProxyUsername(), configuration.getProxyPassword())
);
// Attach Credentials provider to client builder.
clientBuilder.setDefaultCredentialsProvider(credsProvider);
}
// Attach Proxy to request config builder
requestConfigBuilder.setProxy(proxyHost);
}
// Attach default request config
clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
// build http client
httpClient = clientBuilder.build();
}