How to ignore SSL certificate errors in Apache HttpClient 4.4
2015-06-05 14:17
417 查看
public static CloseableHttpClient acceptsUntrustedCertsHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { HttpClientBuilder b = HttpClientBuilder.create(); // setup a Trust Strategy that allows all certificates. // SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { return true; } }).build(); b.setSslcontext( sslContext); // don't check Hostnames, either. // -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE; // here's the special part: // -- need to create an SSL Socket Factory, to use our weakened "trust strategy"; // -- and create a Registry, to register it. // SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", sslSocketFactory) .build(); // now, we create connection-manager using our Registry. // -- allows multi-threaded use PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager( socketFactoryRegistry); b.setConnectionManager( connMgr); // finally, build the HttpClient; // -- done! CloseableHttpClient client = b.build(); return client; }
相关文章推荐
- 网络截取文件名
- iOS开发网络篇—网络编程基础
- iOS开发网络篇—搭建本地服务器
- iOS开发网络篇—HTTP协议
- iOS开发网络篇—GET请求和POST请求
- iOS开发网络篇—NSURLConnection基本使用
- iOS开发网络篇—数据安全
- iOS开发网络篇—多线程断点下载
- iOS开发网络篇—文件的上传
- iOS开发网络篇—简单介绍ASI框架的使用
- iOS开发网络篇—监测网络状态
- iOS开发网络篇—发送json数据给服务器以及多值参数
- iOS开发网络篇 - 数据缓存
- (网络层)IP 协议首部格式与其配套使用的四个协议(ARP,RARP,ICMP,IGMP)
- Android HttpURLConnection 下载xml文件时候 出现ioexception
- ios开发网络篇 - 使用ASI框架进行文件下载
- 这是成长吗?
- Apache HTTP服务器安装后报:无法启动,因为应用程序的并行配置不正确-(已解决)
- Wireshark抓包介绍和TCP三次握手分析
- 理解TCP序列号(Sequence Number)和确认号(Acknowledgment Number)