备注:本处代码使用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】的更多相关文章

  1. 只要是使用函数file_get_contents访问 https 的网站都要开启

    使用file_get_contents();报错failed to open stream: Unable to find the socket transport "ssl" - ...

  2. Android如何使用WebView访问https的网站

    Android中可以用WebView来访问http和https的网站,但是默认访问https网站时,假如证书不被Android承认,会出现空白页面,且不会有任何提示信息,这时我们必须加多一些配置. 此 ...

  3. Firefox访问https的网站,一直提示不安全

    http://mozilla.com.cn/thread-374897-1-1.html 要激活此功能步骤如下: 在地址栏键入"about:config" 点击“我了解此风险” 在 ...

  4. 使用 requests 访问 HTTPS

    当我们访问 HTTPS 的网站时,需要进行证书验证,在浏览器中可以自动处理验证问题,在 Python 中有以下两种做法: import requests //不进行证书验证,但这种方式会出现警告,如下 ...

  5. idhttp访问HTTPS

    idhttp访问HTTPS 访问一个 WEB 网站,如果采用 HTTP 的话,直接使用 TIdHTTP 这个控件,最简单的用法是: S := IdHTTP1.Get('www.qq.com'); 这里 ...

  6. Delphi 访问https /SSL、OpenSSL

    访问 Web 网站,最简单用法直接使用 TIdHTTP 控件: 例如:AA := IdHTTP1.Get('www.baidu.com.'); 访问 https 的网站,需要 SSL 库. 在 Win ...

  7. c# 中HttpClient访问Https网站

    c# 中HttpClient访问Https网站,加入如下代码: handler = new HttpClientHandler() ;handler.AllowAutoRedirect = true; ...

  8. AFNetworking 原作者都无法解决的问题: 如何使用ip直接访问https网站?

    背景 最近App似乎有报异常是DNS无法解析,尝试解决此问题.搜集到的资料很少,甚至连AFN原作者都判定这可能是一个无解的问题,参见: https://github.com/AFNetworking/ ...

  9. Python使用requests模块访问HTTPS网站报错`certificate verify failed`

    使用requests模块访问HTTPS网站报错: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Nam ...

随机推荐

  1. Going Home HDU - 1533 费用流

    http://acm.hdu.edu.cn/showproblem.php?pid=1533 给一个网格图,每两个点之间的匹配花费为其曼哈顿距离,问给每个的"$m$"匹配到一个&q ...

  2. 使用python脚本批量删除阿里云oss中的mp4文件

    #encoding:utf-8 ''' oss中有一些mp4文件需要删除,首先定位出这些文件放在txt文本中 然后通过python操作oss进行批量删除 ''' import oss2 auth = ...

  3. python多线程threading下载示例

    #coding:utf-8 # windows中测试不通过,下载的图片不完整 # 通过多线程下载图片 import requests import threading class downloader ...

  4. Git系列①之仓库管理互联网托管平台github.com的使用

    互联网项目托管平台github.com的使用 1.安装git客户端 # yum install -y git 配置git全局用户以及邮箱 [root@web01 ~]# git config --gl ...

  5. UML教程

    1.前言 1.1 前言   本资料对UML1.5各种模型图的构成和功能进行说明,通过本资料的学习达到可以读懂UML模型图的目的.本资料不涉及模型图作成的要点等相关知识. 1.2 UML概述 1.2.1 ...

  6. PYTHON深拷贝与浅拷贝

    浅拷贝就是对引用的拷贝,深拷贝就是对对象的资源的拷贝 浅拷贝 浅拷贝仅仅复制了容器中元素的地址 赋值的原则 1.赋值是将一个对象的地址赋值给一个变量,让变量指向该地址( 旧瓶装旧酒 ). 2.修改不可 ...

  7. 【原创】大数据基础之Benchmark(2)TPC-DS

    tpc 官方:http://www.tpc.org/ 一 简介 The TPC is a non-profit corporation founded to define transaction pr ...

  8. django.db.utils.OperationalError: (1049, "Unknown database 'djangodb'")

    DATABASES = { 'default': { 'ENGINE':'django.db.backends.mysql', 'NAME': 'mysql', 'USER':'root', 'PAS ...

  9. [Linux]php+apache 和 php+nginx的区别

    apache是通过mod_php来解析php nginx是通过php-fpm(fast-cgi)来解析php1. PHP 解释器是否嵌入 Web 服务器进程内部执行mod_php 通过嵌入 PHP 解 ...

  10. VBS将本地的Excel数据导入到SQL Server中

    VBS将本地的Excel数据导入到SQL Server中 高文龙关注0人评论1170人阅读2017-05-14 12:54:44 VBS将本地的Excel数据导入到SQL Server中 最近有个测试 ...