当前位置: 首页>>代码示例>>Java>>正文


Java ConnectionConfig.Builder方法代码示例

本文整理汇总了Java中org.apache.http.config.ConnectionConfig.Builder方法的典型用法代码示例。如果您正苦于以下问题:Java ConnectionConfig.Builder方法的具体用法?Java ConnectionConfig.Builder怎么用?Java ConnectionConfig.Builder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.http.config.ConnectionConfig的用法示例。


在下文中一共展示了ConnectionConfig.Builder方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testPrepareUserAgentHeaderSetOnBuilder

import org.apache.http.config.ConnectionConfig; //导入方法依赖的package包/类
@Test
public void testPrepareUserAgentHeaderSetOnBuilder() {
  // Setup
  String expectedUserAgentHeader = "Nexus/Agent my user agent";
  HttpClientPlan plan = mock(HttpClientPlan.class);
  doReturn(expectedUserAgentHeader).when(plan).getUserAgent();
  HttpClientBuilder builder = mock(HttpClientBuilder.class);
  doReturn(builder).when(plan).getClient();

  ConnectionConfig.Builder conn = mock(ConnectionConfig.Builder.class);
  SocketConfig.Builder sock = mock(SocketConfig.Builder.class);
  RequestConfig.Builder req = mock(RequestConfig.Builder.class);
  doReturn(null).when(conn).build();
  doReturn(null).when(sock).build();
  doReturn(null).when(req).build();
  doReturn(conn).when(plan).getConnection();
  doReturn(sock).when(plan).getSocket();
  doReturn(req).when(plan).getRequest();

  HttpClientManagerImpl spy = spy(underTest);
  doReturn(plan).when(spy).httpClientPlan();

  // Execute
  HttpClientBuilder returned = spy.prepare(null);

  // Verify
  assertNotNull("Returned builder must not be null.", returned);
  assertEquals("Returned builder must be expected builder.", builder, returned);
  verify(spy).setUserAgent(builder, expectedUserAgentHeader);
}
 
开发者ID:sonatype,项目名称:nexus-public,代码行数:31,代码来源:HttpClientManagerImplTest.java

示例2: createHttpClient

import org.apache.http.config.ConnectionConfig; //导入方法依赖的package包/类
public static HttpClient createHttpClient() {
	HttpClient result = null;
	try {
		// result = HttpClientBuilder.create().build();
		// result = HttpClients.createDefault();

		// HttpClientBuilder
		HttpClientBuilder builder = HttpClientBuilder.create();
		builder.setMaxConnPerRoute(100);
		builder.setMaxConnTotal(200);
		builder.setRetryHandler(new DefaultHttpRequestRetryHandler());

		// RequestConfig
		RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
		requestConfigBuilder.setConnectTimeout(5 * 1000);
		requestConfigBuilder.setSocketTimeout(5 * 1000);
		//
		RequestConfig requestConfig = requestConfigBuilder.build();
		builder.setDefaultRequestConfig(requestConfig);

		// ConnectionConfig
		ConnectionConfig.Builder connectionConfigBuilder = ConnectionConfig
				.custom();
		connectionConfigBuilder.setBufferSize(8192);
		connectionConfigBuilder.setCharset(Charset.forName("UTF-8"));
		//
		ConnectionConfig connectionConfig = connectionConfigBuilder.build();
		builder.setDefaultConnectionConfig(connectionConfig);
		//
		result = builder.build();

	} catch (Exception ex) {
		ex.printStackTrace();
	}
	return result;
}
 
开发者ID:mixaceh,项目名称:openyu-commons,代码行数:37,代码来源:BenchmarkHttpClient4Test.java

示例3: getHttpClientBuilder

import org.apache.http.config.ConnectionConfig; //导入方法依赖的package包/类
public static HttpClientBuilder getHttpClientBuilder(final String charset, String proxyIp,
        int proxyPort) {

    HttpClientBuilder builder = HttpClients.custom();

    Charset chartset = charset == null ? Charset.forName(CHARSET_UTF8)
            : Charset.forName(charset);
    ConnectionConfig.Builder connBuilder = ConnectionConfig.custom().setCharset(chartset);

    RequestConfig.Builder reqBuilder = RequestConfig.custom();
    reqBuilder.setExpectContinueEnabled(false);
    reqBuilder.setSocketTimeout(10 * 60 * 1000);
    reqBuilder.setConnectTimeout(10 * 60 * 1000);
    reqBuilder.setMaxRedirects(10);

    if (StringUtils.isNotBlank(proxyIp) && proxyPort > 0) {
        logger.info("using proxy {}:{} to request ", proxyIp, String.valueOf(proxyPort));
        HttpHost proxy = new HttpHost(proxyIp, proxyPort);
        reqBuilder.setProxy(proxy);
    }

    ServiceUnavailableRetryStrategy serviceUnavailableRetryStrategy = new DefaultServiceUnavailableRetryStrategy(
            3, 3000);
    builder.setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy);
    // 模拟浏览器,解决一些服务器程序只允许浏览器访问的问题
    builder.setUserAgent(
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");

    builder.setDefaultRequestConfig(reqBuilder.build());
    builder.setDefaultConnectionConfig(connBuilder.build());
    builder.setConnectionManager(getConnectionManager());

    // HostnameVerifier allowAllHostnameVerifier =
    // NoopHostnameVerifier.INSTANCE;
    // builder.setSSLHostnameVerifier(allowAllHostnameVerifier);

    return builder;

}
 
开发者ID:jiucai,项目名称:appframework,代码行数:40,代码来源:HttpClientUtil.java

示例4: getClient

import org.apache.http.config.ConnectionConfig; //导入方法依赖的package包/类
/**
 * Get client.
 *
 * @return the client
 */
private synchronized CloseableHttpClient getClient() {
    if (client == null) {
        RequestConfig.Builder requestBuilder = RequestConfig.custom();
        requestBuilder.setConnectTimeout(connectionTimeout);

        ConnectionConfig.Builder connBuilder = ConnectionConfig.custom();
        connBuilder.setCharset(Charset.forName(getContentCharset()));

        // Create and initialize scheme registry
        RegistryBuilder<ConnectionSocketFactory> builder = RegistryBuilder.create();
        builder.register("http", getPlainFactory());
        builder.register("https", getSslFactory());
        Registry<ConnectionSocketFactory> registry = builder.build();

        HttpClientConnectionManager hccm = createClientConnectionManager(registry);

        HttpClientBuilder clientBuilder = HttpClients.custom();
        clientBuilder.setDefaultRequestConfig(requestBuilder.build());
        clientBuilder.setDefaultConnectionConfig(connBuilder.build());
        clientBuilder.setConnectionManager(hccm);

        client = clientBuilder.build();
    }

    return client;
}
 
开发者ID:GoogleCloudPlatform,项目名称:appengine-tck,代码行数:32,代码来源:EndPointClient.java

示例5: getConnection

import org.apache.http.config.ConnectionConfig; //导入方法依赖的package包/类
public ConnectionConfig.Builder getConnection() {
  return connection;
}
 
开发者ID:sonatype,项目名称:nexus-public,代码行数:4,代码来源:HttpClientPlan.java

示例6: buildConnectionManager

import org.apache.http.config.ConnectionConfig; //导入方法依赖的package包/类
/**
 * Builds the HttpClientConnectionManager.
 *
 * <p>Note that this calls {@link #buildSslConnectionSocketFactory()} and registers the resulting {@link SSLConnectionSocketFactory}
 * (if non-null) with its socket factory registry.</p>
 *
 * @return the HttpClientConnectionManager
 */
protected HttpClientConnectionManager buildConnectionManager() {
    PoolingHttpClientConnectionManager poolingConnectionManager = null;

    SSLConnectionSocketFactory sslConnectionSocketFactory = buildSslConnectionSocketFactory();
    if (sslConnectionSocketFactory != null) {
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
                .<ConnectionSocketFactory> create().register("https", sslConnectionSocketFactory)
                .register("http", new PlainConnectionSocketFactory())
                .build();
        poolingConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    } else {
        poolingConnectionManager = new PoolingHttpClientConnectionManager();
    }

    // Configure the connection manager
    poolingConnectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS.getValueOrDefault(DEFAULT_MAX_TOTAL_CONNECTIONS));

    // By default we'll set the max connections per route (essentially that means per host for us) to the max total
    poolingConnectionManager.setDefaultMaxPerRoute(MAX_TOTAL_CONNECTIONS.getValueOrDefault(DEFAULT_MAX_TOTAL_CONNECTIONS));


    SocketConfig.Builder socketConfigBuilder = SocketConfig.custom();
    socketConfigBuilder.setSoTimeout(SO_TIMEOUT.getValueOrDefault(DEFAULT_SOCKET_TIMEOUT));

    Integer soLinger = SO_LINGER.getValue();
    if (soLinger != null) {
        socketConfigBuilder.setSoLinger(soLinger);
    }

    Boolean isTcpNoDelay = TCP_NODELAY.getValue();
    if (isTcpNoDelay != null) {
        socketConfigBuilder.setTcpNoDelay(isTcpNoDelay);
    }

    poolingConnectionManager.setDefaultSocketConfig(socketConfigBuilder.build());

    ConnectionConfig.Builder connectionConfigBuilder = ConnectionConfig.custom();

    Integer sendBuffer = SO_SNDBUF.getValue();
    Integer receiveBuffer = SO_RCVBUF.getValue();

    // if either send or recieve buffer size is set, we'll set the buffer size to whichever is greater
    if (sendBuffer != null || receiveBuffer != null) {
        Integer bufferSize = -1;
        if (sendBuffer != null) {
            bufferSize = sendBuffer;
        }

        if (receiveBuffer != null && receiveBuffer > bufferSize) {
            bufferSize = receiveBuffer;
        }

        connectionConfigBuilder.setBufferSize(bufferSize);
    }

    String contentCharset = HTTP_CONTENT_CHARSET.getValue();
    if (contentCharset != null) {
        connectionConfigBuilder.setCharset(Charset.forName(contentCharset));
    }

    poolingConnectionManager.setDefaultConnectionConfig(connectionConfigBuilder.build());

    return poolingConnectionManager;
}
 
开发者ID:kuali,项目名称:rice,代码行数:73,代码来源:DefaultHttpClientConfigurer.java


注:本文中的org.apache.http.config.ConnectionConfig.Builder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。