當前位置: 首頁>>代碼示例>>Java>>正文


Java RequestConfig.Builder方法代碼示例

本文整理匯總了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;
}
 
開發者ID:aliyun,項目名稱:HiTSDB-Client,代碼行數:20,代碼來源:HttpClientFactory.java

示例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);

}
 
開發者ID:NationalSecurityAgency,項目名稱:qonduit,代碼行數:37,代碼來源:HTTPStrictTransportSecurityIT.java

示例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();
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:19,代碼來源:RestClientBuilder.java

示例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();
}
 
開發者ID:LeeKyoungIl,項目名稱:illuminati,代碼行數:22,代碼來源:IlluminatiHttpClient.java

示例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));
}
 
開發者ID:AlexanderBartash,項目名稱:hybris-integration-intellij-idea-plugin,代碼行數:18,代碼來源:StatsRequest.java

示例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();

}
 
開發者ID:wxz1211,項目名稱:dooo,代碼行數:24,代碼來源:IpClient.java

示例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());
}
 
開發者ID:spring-projects,項目名稱:spring-credhub,代碼行數:22,代碼來源:ClientHttpRequestFactoryFactory.java

示例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());
}
 
開發者ID:IBM,項目名稱:ibm-cos-sdk-java,代碼行數:26,代碼來源:ApacheHttpRequestFactory.java

示例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;
}
 
開發者ID:brucezee,項目名稱:jspider,代碼行數:15,代碼來源:HttpClientFactory.java

示例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;
}
 
開發者ID:xbynet,項目名稱:crawler,代碼行數:28,代碼來源:AbsDownloader.java

示例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();
}
 
開發者ID:bcgov,項目名稱:nifi-atlas,代碼行數:13,代碼來源:NiFiApiClient.java

示例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();
}
 
開發者ID:fengzhizi715,項目名稱:NetDiscovery,代碼行數:45,代碼來源:HttpManager.java

示例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();
}
 
開發者ID:fengzhizi715,項目名稱:PicCrawler,代碼行數:45,代碼來源:HttpManager.java

示例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;

}
 
開發者ID:wu05281,項目名稱:admin-shiro,代碼行數:62,代碼來源:RestTemplateConfig.java

示例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();
}
 
開發者ID:vy,項目名稱:hrrs,代碼行數:11,代碼來源:ApacheHttpClientFactory.java


注:本文中的org.apache.http.client.config.RequestConfig.Builder方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。