HttpClient https认证
来源:互联网 发布:sqlalchemy sql输出 编辑:程序博客网 时间:2024/06/09 23:33
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初始化完毕");
0 0
- HttpClient https认证
- httpclient 面https认证,带cookie访问
- Android在使用HttpClient访问https时认证策略
- Android在使用HttpClient访问https时认证策略
- java 通过Httpclient模拟https Post 请求,实现双向认证
- httpclient认证
- HttpClient详细实例,Httoclient调用https实例,避免https SSL认证
- HTTPS HTTPClient
- httpclient https
- https认证
- https认证
- HttpClient ssl双向认证
- httpclient双向认证
- HttpClient认证机制
- HttpClient 认证机制
- httpclient basic认证
- HttpClient认证机制
- elasticsearch httpclient认证机制
- C# Hashtable 使用说明 以及 Hashtable和HashMap的区别
- python+robotframework --环境安装+配置
- 一文学会用 Tensorflow 搭建神经网络
- 断是否是周六周日
- 山科校赛 Problem A: 字符串“水”题(状态压缩)
- HttpClient https认证
- json fasterxml date 日期格式 设置
- 实现自己的Handler、Looper、MessageQueue
- MySQL 基本的知识
- 数据仓库中的Inmon与Kimball架构之争
- Tiny6410内核编译、内核模块编译
- Pcap.h no such file and directory
- DAG上的动态规划
- include指令