本地测试没问题,http换成https抛出异常javax.net.ssl.SSLHandshakeException,网上有说是服务器证书,有说要启动SSL3协议的,反正没有找到有用的。

在GET和POST请求时创建安全的SSL连接:

代码:

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; /**
* <p>HTTP 请求工具类<p>
* @version 1.0
* @author li_hao
* @date 2018年11月7日
*/
public class HttpSendUtil {
private static PoolingHttpClientConnectionManager connMgr;
private static RequestConfig requestConfig;
private static final int MAX_TIMEOUT = 600000; static {
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", createSSLConnSocketFactory())
.build();
// 设置连接池
connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
// 设置连接池大小
connMgr.setMaxTotal(100);
connMgr.setDefaultMaxPerRoute(connMgr.getMaxTotal());
RequestConfig.Builder configBuilder = RequestConfig.custom();
// 设置连接超时
configBuilder.setConnectTimeout(MAX_TIMEOUT);
// 设置读取超时
configBuilder.setSocketTimeout(MAX_TIMEOUT);
// 设置从连接池获取连接实例的超时
configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT);
// 在提交请求之前 测试连接是否可用
configBuilder.setStaleConnectionCheckEnabled(true);
requestConfig = configBuilder.build();
} /**
* 发送 GET 请求(HTTP)
*
* @param url
* @return
*/
public static JSONObject doGet(String url) {
CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = null;
String out = null;
JSONObject jsonObject = null;//接收结果
try {
response = httpclient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) { //请求出错
System.out.println(EntityUtils.toString(response.getEntity(), "utf-8") + url); //打印错误信息
return null;
}
out = EntityUtils.toString(response.getEntity(), "utf-8");
jsonObject = JSONObject.parseObject(out);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpGet != null) {
httpGet.releaseConnection();
}
}
return jsonObject;
} /**
* 发送 POST 请求
*
* @param url API接口URL
* @param params 参数map
* @return
*/
public static JSONObject doPost(String url, String params) {
CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
String out = null;
JSONObject jsonObject = null;//接收结果
try {
httpPost.setConfig(requestConfig);
StringEntity stringEntity = new StringEntity(params, "UTF-8");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity); response = httpclient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
System.out.println(EntityUtils.toString(response.getEntity(), "utf-8") + "url: " +url + "params: " + params); //打印错误信息
return null;
}
out = EntityUtils.toString(response.getEntity(), "utf-8");
jsonObject = JSONObject.parseObject(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpPost != null) {
httpPost.releaseConnection();
}
}
return jsonObject;
} /**
* 创建SSL安全连接
*
* @return
*/
private static SSLConnectionSocketFactory createSSLConnSocketFactory() {
SSLConnectionSocketFactory sslsf = null;
try {
SSLContext ctx = SSLContext.getInstance("SSL");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
} @Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
} @Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
sslsf = new SSLConnectionSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
return sslsf;
}
}

这样一来,访问HTTPS就不抛 javax.net.ssl.SSLHandshakeException异常了。

解决访问HTTPS,抛出的异常javax.net.ssl.SSLHandshakeException的更多相关文章

  1. druid抛出的异常------javax.management.InstanceAlreadyExistsException引发的一系列探索

    最近项目中有个定时任务的需求,定时检查mysql数据与etcd数据的一致性,具体实现细节就不说了,今天要说的就是实现过程中遇到了druid抛出的异常,以及解决的过程 异常 异常详细信息 五月 05, ...

  2. 解决Java调用Azure SDK证书错误javax.net.ssl.SSLHandshakeException

    Azure作为微软的公有云平台,提供了非常丰富的SDK和API让开发人员可以非常方便的调用的各项服务,目前除了自家的.NET, Java, Python, nodeJS, Ruby,PHP等语言都提供 ...

  3. 解决 Java 调用 Azure SDK 证书错误 javax.net.ssl.SSLHandshakeException

    Azure 作为微软的公有云平台,提供了非常丰富的 SDK 和 API 让开发人员可以非常方便的调用的各项服务,目前除了自家的 .NET.Java.Python. nodeJS.Ruby,PHP 等语 ...

  4. selenium 访问网页抛出ElementNotVisibleException异常

    问题描述: 在使用selenium时遇到如下异常导致程序终止: selenium.common.exceptions.ElementNotVisibleException: Message: {&qu ...

  5. SSL异常javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

    jdk 7 http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html jdk 8 http: ...

  6. javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certificatio

    场景:Java调用PHP接口,代码部署在服务器上后,调用报错,显示PHP服务器那边证书我这边服务器不信任(我猜的). 异常信息: 2019-08-06 14:00:09,102 [http-nio-4 ...

  7. jdk1.7访问https报javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure问题解决

    本地jdk版本java version "1.8.0_31",代码中已对https做了相应处理:信任所有来源证书,运行正常:上包到服务器(服务器jdk版本java version ...

  8. 异常信息:javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed

    上周五遇到一个问题,工程本地编译运行正常,打包本地tomcat运行也正常.部署到测试环境报错: 2017-05-05 09:38:11.645 ERROR [HttpPoolClientsUtil.j ...

  9. javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed

    1.使用HttpClient4.3 调用https出现如下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.Validat ...

随机推荐

  1. C89 和 C99 标准比较

    注1: GCC支持C99, 通过 --std=c99 命令行参数开启,如: 代码:gcc --std=c99 test.c    注2:FFMPEG使用的是C99.而VC支持的是C89(不支持C99) ...

  2. [UE4]用Format Text进行调试

    {姓名},在{时间}进来了 “{姓名}”和“{时间}”会自动变成一个变量.

  3. javascript多语言设定方式

    1.控件文字语言设定 1.1 设定两个结构变量,定义好每一个控件需要用到的值,称它字典对象 var languagecn = { 1089: "夹柄不到位", 1090: &quo ...

  4. Git 分布式版本管理器 windows环境下使用

    首先需要下载Git来安装  没有安装文件的小伙伴可以网盘下载——>   https://pan.baidu.com/s/1owwUItDri9skqYzOjzXLsw 之后安装Git  一路很顺 ...

  5. 数据泵导出报ORA-01555 ORA-22924

    最近对某数据库执行数据泵导出操作时碰到如下错误:Processing object type DATABASE_EXPORT/SCHEMA/JOBProcessing object type DATA ...

  6. 微信小程序web-view的简单思考和实践

    微信小程序的组件web-view推出有一段时间了,这个组件的推出可以说是微信小程序开发的一个重要事件,让微信小程序不会只束缚在微信圈子里了,打开了一个口子,这个口子或许还比较小,但未来有无限可能. 简 ...

  7. html5 video标签屏蔽右键视频另存为的js代码-HTML5教程

    点评:html5 video标签本身有下载功能但是在video区域内,点击右键可以将“视频另存为”下面是屏蔽右键视频另存为的js代码,有此需求的朋友不要错过   做HTML5的video标签,本身我们 ...

  8. docker学习记录

    Container 容器是一种基础工具, 泛指任何容纳其他物品的工具, 可以部分或者完全封闭,被用于容纳,储存, 运输物品, 物体可以放置在容器中, 而容器可以保护内容物 1 Docker Objec ...

  9. JSON.stringify()的深度使用

    在使用JSON.stringify()对JSON数据进行序列化时 1> 如果里面的属性是function,则会被忽略 const data = { a: 'a', fn: funciton() ...

  10. Navicat操作SQL server 2008R2文件.bak文件还原

    项目操作过程中,利用Navicat操作SQL Server2008R2数据备份,结果发现数据丢失了很多,不得不先对数据丢失部分进行差异对比,然后再重新输入. 1.利用Navicat导出的数据格式为sq ...