本文整理汇总了Java中org.apache.http.client.config.RequestConfig.Builder方法的典型用法代码示例。如果您正苦于以下问题:Java RequestConfig.Builder方法的具体用法?Java RequestConfig.Builder怎么用?Java RequestConfig.Builder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.client.config.RequestConfig
的用法示例。
在下文中一共展示了RequestConfig.Builder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initRequestConfig
import org.apache.http.client.config.RequestConfig; //导入方法依赖的package包/类
private static RequestConfig initRequestConfig(HiTSDBConfig config) {
RequestConfig requestConfig = null;
// 设置请求
int httpConnectTimeout = config.getHttpConnectTimeout();
// 需要设置
if (httpConnectTimeout >= 0) {
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
// ConnectTimeout:连接超时.连接建立时间,三次握手完成时间.
requestConfigBuilder.setConnectTimeout(httpConnectTimeout * 1000);
// SocketTimeout:Socket请求超时.数据传输过程中数据包之间间隔的最大时间.
requestConfigBuilder.setSocketTimeout(httpConnectTimeout * 1000);
// ConnectionRequestTimeout:httpclient使用连接池来管理连接,这个时间就是从连接池获取连接的超时时间,可以想象下数据库连接池
requestConfigBuilder.setConnectionRequestTimeout(httpConnectTimeout * 1000);
requestConfig = requestConfigBuilder.build();
}
return requestConfig;
}
示例2: testHttpRequestGet
import org.apache.http.client.config.RequestConfig; //导入方法依赖的package包/类
@Test
public void testHttpRequestGet() throws Exception {
RequestConfig.Builder req = RequestConfig.custom();
req.setConnectTimeout(5000);
req.setConnectionRequestTimeout(5000);
req.setRedirectsEnabled(false);
req.setSocketTimeout(5000);
req.setExpectContinueEnabled(false);
HttpGet get = new HttpGet("http://127.0.0.1:54322/login");
get.setConfig(req.build());
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setDefaultMaxPerRoute(5);
HttpClientBuilder builder = HttpClients.custom();
builder.disableAutomaticRetries();
builder.disableRedirectHandling();
builder.setConnectionTimeToLive(5, TimeUnit.SECONDS);
builder.setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE);
builder.setConnectionManager(cm);
CloseableHttpClient client = builder.build();
String s = client.execute(get, new ResponseHandler<String>() {
@Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
assertEquals(301, response.getStatusLine().getStatusCode());
return "success";
}
});
assertEquals("success", s);
}
示例3: createHttpClient
import org.apache.http.client.config.RequestConfig; //导入方法依赖的package包/类
private CloseableHttpAsyncClient createHttpClient() {
//default timeouts are all infinite
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS)
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT_MILLIS)
.setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT_MILLIS);
if (requestConfigCallback != null) {
requestConfigBuilder = requestConfigCallback.customizeRequestConfig(requestConfigBuilder);
}
HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClientBuilder.create().setDefaultRequestConfig(requestConfigBuilder.build())
//default settings for connection pooling may be too constraining
.setMaxConnPerRoute(DEFAULT_MAX_CONN_PER_ROUTE).setMaxConnTotal(DEFAULT_MAX_CONN_TOTAL);
if (httpClientConfigCallback != null) {
httpClientBuilder = httpClientConfigCallback.customizeHttpClient(httpClientBuilder);
}
return httpClientBuilder.build();
}
示例4: initPoolingHttpClientManager
import org.apache.http.client.config.RequestConfig; //导入方法依赖的package包/类
private void initPoolingHttpClientManager () {
final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.register("http", PlainConnectionSocketFactory.getSocketFactory()).build();
final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connectionManager.setMaxTotal(MAX_CONNECTION);
connectionManager.setDefaultMaxPerRoute(MAX_CONNECTION_PER_ROUTE);
final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setConnectTimeout(CONNECTION_TIMEOUT)
.setConnectionRequestTimeout(CONNECTION_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT);
final RequestConfig requestConfig = requestConfigBuilder.build();
HashSet<Header> defaultHeaders = new HashSet<Header>();
defaultHeaders.add(new BasicHeader(HttpHeaders.PRAGMA, "no-cache"));
defaultHeaders.add(new BasicHeader(HttpHeaders.CACHE_CONTROL, "no-cache"));
final HttpClientBuilder httpClientBuilder = HttpClients.custom().setDefaultHeaders(defaultHeaders).disableAuthCaching().disableContentCompression();
this.httpClient = httpClientBuilder.setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build();
}
示例5: call
import org.apache.http.client.config.RequestConfig; //导入方法依赖的package包/类
@Override
public StatsResponse call() throws Exception {
final RequestConfig.Builder builder = RequestConfig
.custom()
.setConnectTimeout(TIMEOUT)
.setConnectionRequestTimeout(TIMEOUT)
.setSocketTimeout(TIMEOUT);
IdeHttpClientHelpers.ApacheHttpClient4.setProxyForUrlIfEnabled(builder, HybrisConstants.STATS_COLLECTOR_URL);
final RequestConfig config = builder.build();
final HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
final List<NameValuePair> patchedUrlParameters = ContainerUtil.newArrayList(urlParameters);
patchUrlParameters(patchedUrlParameters);
final HttpPost post = new HttpPost(HybrisConstants.STATS_COLLECTOR_URL);
post.setEntity(new UrlEncodedFormEntity(patchedUrlParameters, UTF_8));
return new StatsResponse(client.execute(post));
}
示例6: post0
import org.apache.http.client.config.RequestConfig; //导入方法依赖的package包/类
private static HttpUriRequest post0(String url, Map<String, String> params) {
RequestBuilder requestBuilder = RequestBuilder.post().setCharset(CHARSET).setUri(url);
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
.setConnectionRequestTimeout(TIMEOUT)
.setSocketTimeout(TIMEOUT)
.setConnectTimeout(TIMEOUT).setCookieSpec(CookieSpecs.IGNORE_COOKIES);
requestBuilder.setConfig(requestConfigBuilder.build());
if (params != null && params.size() > 0) {
List<NameValuePair> nvList = new ArrayList<>();
for (Map.Entry<String, String> param : params.entrySet()) {
Object value = param.getValue();
if (value == null) {
continue;
}
nvList.add(new BasicNameValuePair(param.getKey(), value.toString()));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvList, CHARSET);
requestBuilder.setEntity(formEntity);
}
requestBuilder.addHeader("Connection", "Keep-Alive");
return requestBuilder.build();
}
示例7: usingHttpComponents
import org.apache.http.client.config.RequestConfig; //导入方法依赖的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());
}
示例8: addRequestConfig
import org.apache.http.client.config.RequestConfig; //导入方法依赖的package包/类
private void addRequestConfig(final HttpRequestBase base,
final Request<?> request,
final HttpClientSettings settings) {
final RequestConfig.Builder requestConfigBuilder = RequestConfig
.custom()
.setConnectionRequestTimeout(settings.getConnectionPoolRequestTimeout())
.setConnectTimeout(settings.getConnectionTimeout())
.setSocketTimeout(settings.getSocketTimeout())
.setLocalAddress(settings.getLocalAddress());
/*
* Enable 100-continue support for PUT operations, since this is
* where we're potentially uploading large amounts of data and want
* to find out as early as possible if an operation will fail. We
* don't want to do this for all operations since it will cause
* extra latency in the network interaction.
*/
if (HttpMethodName.PUT == request.getHttpMethod() && settings.isUseExpectContinue()) {
requestConfigBuilder.setExpectContinueEnabled(true);
}
addProxyConfig(requestConfigBuilder, settings);
base.setConfig(requestConfigBuilder.build());
}
示例9: createRequestConfigBuilder
import org.apache.http.client.config.RequestConfig; //导入方法依赖的package包/类
public RequestConfig.Builder createRequestConfigBuilder(SiteConfig siteConfig, Request request, HttpHost proxy) {
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
requestConfigBuilder.setConnectTimeout(siteConfig.getConnectTimeout());
requestConfigBuilder.setSocketTimeout(siteConfig.getSocketTimeout());
requestConfigBuilder.setRedirectsEnabled(siteConfig.isRedirectsEnabled());
requestConfigBuilder.setConnectionRequestTimeout(siteConfig.getConnectionRequestTimeout());
requestConfigBuilder.setCircularRedirectsAllowed(siteConfig.isCircularRedirectsAllowed());
requestConfigBuilder.setMaxRedirects(siteConfig.getMaxRedirects());
requestConfigBuilder.setCookieSpec(siteConfig.getCookieSpec());
requestConfigBuilder.setProxy(proxy);
return requestConfigBuilder;
}
示例10: generateHttpRequest
import org.apache.http.client.config.RequestConfig; //导入方法依赖的package包/类
protected HttpUriRequest generateHttpRequest(Site site,Request request,HttpHost proxy){
RequestBuilder requestBuilder = selectRequestMethod(request).setUri(request.getUrl());
if (site.getHeaders() != null) {
for (Map.Entry<String, String> headerEntry : site.getHeaders().entrySet()) {
requestBuilder.setHeader(headerEntry.getKey(), headerEntry.getValue());
}
}
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
if (site != null) {
requestConfigBuilder.setConnectionRequestTimeout(site.getTimeout())
.setSocketTimeout(site.getTimeout())
.setConnectTimeout(site.getTimeout())
.setCookieSpec(CookieSpecs.STANDARD);
}
if (proxy != null) {
requestConfigBuilder.setProxy(proxy);
}
requestBuilder.setConfig(requestConfigBuilder.build());
HttpUriRequest httpUriRequest = requestBuilder.build();
if (request.getHeaders() != null && !request.getHeaders().isEmpty()) {
for (Map.Entry<String, String> header : request.getHeaders().entrySet()) {
httpUriRequest.setHeader(header.getKey(), header.getValue());
}
}
return httpUriRequest;
}
示例11: NiFiApiClient
import org.apache.http.client.config.RequestConfig; //导入方法依赖的package包/类
public NiFiApiClient(String baseUri) {
this.baseUri = baseUri.endsWith("/") ? baseUri : baseUri + "/";
final int connectTimeoutMillis = 3000;
final int readTimeoutMillis = 3000;
final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
.setConnectionRequestTimeout(connectTimeoutMillis)
.setConnectTimeout(connectTimeoutMillis)
.setSocketTimeout(readTimeoutMillis);
requestConfig = requestConfigBuilder.build();
}
示例12: createHttpClient
import org.apache.http.client.config.RequestConfig; //导入方法依赖的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();
}
示例13: createHttpClient
import org.apache.http.client.config.RequestConfig; //导入方法依赖的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();
}
示例14: initRestTemplate
import org.apache.http.client.config.RequestConfig; //导入方法依赖的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;
}
示例15: createRequestConfig
import org.apache.http.client.config.RequestConfig; //导入方法依赖的package包/类
private RequestConfig createRequestConfig() {
int requestTimeoutMillis = (int) (config.getRequestTimeoutSeconds() * 1000);
RequestConfig.Builder builder = RequestConfig
.custom()
.setConnectTimeout(requestTimeoutMillis)
.setConnectionRequestTimeout(requestTimeoutMillis)
.setSocketTimeout(requestTimeoutMillis);
setLocalAddress(builder);
return builder.build();
}