本文整理汇总了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);
}
示例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;
}
示例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;
}
示例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;
}
示例5: getConnection
import org.apache.http.config.ConnectionConfig; //导入方法依赖的package包/类
public ConnectionConfig.Builder getConnection() {
return connection;
}
示例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;
}