logger.info("正在初始化HttpClient");
// 自定义解析,选择默认解析
HttpMessageParserFactory<HttpResponse> responseParserFactory = new DefaultHttpResponseParserFactory();
HttpMessageWriterFactory<HttpRequest> requestWriterFactory = new DefaultHttpRequestWriterFactory();
// 利用ParserFactory创建连接工厂
HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory(
requestWriterFactory, responseParserFactory);
// SSL配置,获取默认SSL
SSLContext sslcontext = SSLContexts.createSystemDefault();
// 注册协议
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", new SSLConnectionSocketFactory(sslcontext)).build();
// 自定义DNS
DnsResolver dnsResolver = new SystemDefaultDnsResolver() {
@Override
public InetAddress[] resolve(final String host) throws UnknownHostException {
if (host.equalsIgnoreCase("myhost") || host.equalsIgnoreCase("localhost")) {
return new InetAddress[] { InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }) };
} else {
return super.resolve(host);
}
}
};
// 连接池管理
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry,
connFactory, dnsResolver);
// socket配置,不延迟发送
SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
// 将socket配置设置为连接池默认配置
connManager.setDefaultSocketConfig(socketConfig);
// 暂停活动1S后验证连接
connManager.setValidateAfterInactivity(1000);
// 消息容器,初始化消息容器以及消息容器的配置
MessageConstraints messageConstraints = MessageConstraints.custom().setMaxHeaderCount(200)
.setMaxLineLength(2000).build();
Charset charset = null;
charset = Charset.forName("UTF8");
logger.info("ConnectionConfig-charset为:" + charset);
// Create connection configuration
ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE)
.setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(charset)
.setMessageConstraints(messageConstraints).build();
// Configure the connection manager to use connection configuration
// either
// by default or for a specific host.
connManager.setDefaultConnectionConfig(connectionConfig);
// Configure total max or per route limits for persistent connections
// that can be kept in the pool or leased by the connection manager.
connManager.setMaxTotal(100);
connManager.setDefaultMaxPerRoute(10);
// Use custom cookie store if necessary.
cookieStore = new BasicCookieStore();
// Create global request configuration
RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT)
.setExpectContinueEnabled(true)
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build();
// Create an HttpClient with the given custom dependencies and
// configuration.
httpclient = HttpClients.custom().setConnectionManager(connManager).setDefaultCookieStore(cookieStore)
.setDefaultRequestConfig(defaultRequestConfig).build();
logger.info("HttpClient初始化完毕");
// 自定义解析,选择默认解析
HttpMessageParserFactory<HttpResponse> responseParserFactory = new DefaultHttpResponseParserFactory();
HttpMessageWriterFactory<HttpRequest> requestWriterFactory = new DefaultHttpRequestWriterFactory();
// 利用ParserFactory创建连接工厂
HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory(
requestWriterFactory, responseParserFactory);
// SSL配置,获取默认SSL
SSLContext sslcontext = SSLContexts.createSystemDefault();
// 注册协议
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", new SSLConnectionSocketFactory(sslcontext)).build();
// 自定义DNS
DnsResolver dnsResolver = new SystemDefaultDnsResolver() {
@Override
public InetAddress[] resolve(final String host) throws UnknownHostException {
if (host.equalsIgnoreCase("myhost") || host.equalsIgnoreCase("localhost")) {
return new InetAddress[] { InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }) };
} else {
return super.resolve(host);
}
}
};
// 连接池管理
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry,
connFactory, dnsResolver);
// socket配置,不延迟发送
SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
// 将socket配置设置为连接池默认配置
connManager.setDefaultSocketConfig(socketConfig);
// 暂停活动1S后验证连接
connManager.setValidateAfterInactivity(1000);
// 消息容器,初始化消息容器以及消息容器的配置
MessageConstraints messageConstraints = MessageConstraints.custom().setMaxHeaderCount(200)
.setMaxLineLength(2000).build();
Charset charset = null;
charset = Charset.forName("UTF8");
logger.info("ConnectionConfig-charset为:" + charset);
// Create connection configuration
ConnectionConfig connectionConfig = ConnectionConfig.custom().setMalformedInputAction(CodingErrorAction.IGNORE)
.setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(charset)
.setMessageConstraints(messageConstraints).build();
// Configure the connection manager to use connection configuration
// either
// by default or for a specific host.
connManager.setDefaultConnectionConfig(connectionConfig);
// Configure total max or per route limits for persistent connections
// that can be kept in the pool or leased by the connection manager.
connManager.setMaxTotal(100);
connManager.setDefaultMaxPerRoute(10);
// Use custom cookie store if necessary.
cookieStore = new BasicCookieStore();
// Create global request configuration
RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT)
.setExpectContinueEnabled(true)
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build();
// Create an HttpClient with the given custom dependencies and
// configuration.
httpclient = HttpClients.custom().setConnectionManager(connManager).setDefaultCookieStore(cookieStore)
.setDefaultRequestConfig(defaultRequestConfig).build();
logger.info("HttpClient初始化完毕");