解决httpclient访问ssl资源报证书错误的问题
2016-03-04 10:12
573 查看
创建一个自定义httpclient
访问https资源
public static HttpClient getHttpsClient() { HttpClient httpClient = null; SSLContext context; try { context = SSLContext.getInstance("SSL"); context.init(null, new TrustManager[] {new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] paramArrayOfX509Certificate, String paramString) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }}, new SecureRandom()); HostnameVerifier verifier = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(context, verifier); httpClient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build(); } catch (Exception e) { e.printStackTrace(); } return httpClient; }
访问https资源
HttpClient httpClient = Tool.getHttpsClient(); String url = "https://pic2.zhimg.com/6f7c6805962dc58cc21d4df748f18125_xl.jpg"; HttpGet request = new HttpGet(url); HttpResponse response = httpClient.execute(request);
相关文章推荐
- xutils 3的简单使用
- 轻量级HTTP服务器Nginx(常用配置…
- WEB服务器TCP:time wait&nbsp…
- web性能测试工具-http_load
- HTTP协议详解(真的很经典)
- 网络安全运维
- iOS实际网络检测框架RealReachability
- uploadify3.1--3.2 HTTP&nbsp…
- http请求中application/x-www-form…
- 【HTTP】http重定向301/302/303/307
- 常见HTTP状态码
- Web服务器性能/压力测试工具http_l…
- PHP的$_SERVER['HTTP_H…
- PHP实现http与https转化
- NSCache和NSURLCache网络缓存优化
- 高效的使用okhttp
- BASIC HTTP AUTHENTICATION ON ANDROID
- Web服务器处理HTTP压缩之gzip、deflate压缩
- 网络协议纵览
- Owin的URL编码怎么搞?以前都是HttpUtility.UrlEncode之类的,现在连system.web都没了,肿么办?