如何访问https的网站?-【httpclient】
备注:本处代码使用groovy和httpclient4.3作为例子进行讲述
在普通方式下,当使用httpclient进行访问某个网站时,大致使用如下的代码进行访问:
CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpMethod = new HttpGet(url); response = httpclient.execute(httpMethod);
当使用上述代码来访问https的网站时,就会抛出如下的异常:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
要解决此问题,可以通过如下的方式:
首先创建一个类DefaultTrustManager
class DefaultTrustManager implements X509TrustManager{
@Override
void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
X509Certificate[] getAcceptedIssuers() {
return null
}
}
然后在创建httpclient时,使用如下的代码:
def trustManagers = new TrustManager[1]
trustManagers[0] = new DefaultTrustManager() SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(new KeyManager[0], trustManagers, new SecureRandom());
SSLContext.setDefault(sslContext); sslContext.init(null, trustManagers, null);
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
这样在访问某些https的网站时就能正常访问了。
如何访问https的网站?-【httpclient】的更多相关文章
- 只要是使用函数file_get_contents访问 https 的网站都要开启
使用file_get_contents();报错failed to open stream: Unable to find the socket transport "ssl" - ...
- Android如何使用WebView访问https的网站
Android中可以用WebView来访问http和https的网站,但是默认访问https网站时,假如证书不被Android承认,会出现空白页面,且不会有任何提示信息,这时我们必须加多一些配置. 此 ...
- Firefox访问https的网站,一直提示不安全
http://mozilla.com.cn/thread-374897-1-1.html 要激活此功能步骤如下: 在地址栏键入"about:config" 点击“我了解此风险” 在 ...
- 使用 requests 访问 HTTPS
当我们访问 HTTPS 的网站时,需要进行证书验证,在浏览器中可以自动处理验证问题,在 Python 中有以下两种做法: import requests //不进行证书验证,但这种方式会出现警告,如下 ...
- idhttp访问HTTPS
idhttp访问HTTPS 访问一个 WEB 网站,如果采用 HTTP 的话,直接使用 TIdHTTP 这个控件,最简单的用法是: S := IdHTTP1.Get('www.qq.com'); 这里 ...
- Delphi 访问https /SSL、OpenSSL
访问 Web 网站,最简单用法直接使用 TIdHTTP 控件: 例如:AA := IdHTTP1.Get('www.baidu.com.'); 访问 https 的网站,需要 SSL 库. 在 Win ...
- c# 中HttpClient访问Https网站
c# 中HttpClient访问Https网站,加入如下代码: handler = new HttpClientHandler() ;handler.AllowAutoRedirect = true; ...
- AFNetworking 原作者都无法解决的问题: 如何使用ip直接访问https网站?
背景 最近App似乎有报异常是DNS无法解析,尝试解决此问题.搜集到的资料很少,甚至连AFN原作者都判定这可能是一个无解的问题,参见: https://github.com/AFNetworking/ ...
- Python使用requests模块访问HTTPS网站报错`certificate verify failed`
使用requests模块访问HTTPS网站报错: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Nam ...
随机推荐
- hibernate框架学习第一天:hibernate介绍及基本操作
框架辅助开发者进行开发,半成品软件,开发者与框架进行合作开发 Hibernate3Hibernate是一种基于Java的轻量级的ORM框架 基于Java:底层实现是Java语言,可以脱离WEB,在纯J ...
- web-font 个人学习小总结
个人觉得 web-font 分为两种: 第一种就是真正文本字体,客户端没有安装的字体通过远程加载字体来实现特殊字体提高用户体验: icodon.com : http://icodon.com/goo ...
- Zookeeper-Watcher机制与异步调用原理
转载于:http://shift-alt-ctrl.iteye.com/blog/1847320 Watcher机制:目的是为ZK客户端操作提供一种类似于异步获得数据的操作. 1)在创建Zookeep ...
- Jenkins pipeline概念理解
1.Jenkins Pipeline总体介绍 Pipeline,简而言之,就是一台运行于Jenkins上的工作流框架,将原本独立运行于单个或者多个节点的任务连接起来,实现单个任务难以完成的复杂流程 ...
- mysql设计表时注意事项
说明:本文是对项目过程中的一些要求的简单汇总整理,主要是供个人本身参考... 一.表设计 1. 在创建表结构时,表名.字段需要见名知意,不采用拼音 create table `tb_abc` ( ...
- 【原创】Linux基础之Shell脚本常用命令
#!/bin/sh 1 取脚本参数 $# 参数个数$0 当前脚本名$1 第1个参数$n 第n个参数$* 所有参数$@ 所有参数$? 上个命令的状态$$ 当前pid 2 日期 $ dateWed Mar ...
- 第一篇----mysql体系
mysql体系: 解释: 调用: 1.connectors:连接器 (远程调用mysql,Native很常用的mysql远程连接工具.其它是可以调用mysql支持的一些语言和方法) mysql结构 2 ...
- poj1564 Sum It Up dfs水题
题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...
- swift 实践- 08 -- UISegmentedControl
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...
- Confluence 6 导入模板的备注
创建你自己的模板组件(template bundles).你可以使用插件(add-on,也可以被称 plugin)来创建模板组件然后将这些模板组件上传到你的 Confluence 站点中.你可以从你的 ...