博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java ssl protocol_https Java SSLException protocol_version的问题解决方法
阅读量:5011 次
发布时间:2019-06-12

本文共 2839 字,大约阅读时间需要 9 分钟。

我们的业务代码里有需要用爬虫爬取商品的图片地址,然后在转到我们的服务器里的过程,中间当然少不了下载图片的过程,最近目标网站有些改变,就是之前http前缀的图片地址部分改成了https,然后就造成了一个问题,图片下载不了,查看log,发现一个java的SSLException.

20180813154026314696.png

异常是: java.lang.RuntimeException:Received fatal alert: protocol_version

20180813154026579355.png

异常的根源是:javax.net.ssl.SSLException:  Received fatal alert: protocol_version.

----------------------------------------------------------------------------------------------------------------

原因是: https的请求需要加上ssl的验证,但是代码里一般只用到http,即使用到https也只是换了个前缀,并没有带上ssl验证的过程。

源代码:

private static final PoolingHttpClientConnectionManager HTTP_CLIENT_CONNECTION_MANAGER;

private static final CloseableHttpClient HTTP_CLIENT;

static {

HTTP_CLIENT_CONNECTION_MANAGER = new PoolingHttpClientConnectionManager(RegistryBuilder

. create().register("http", PlainConnectionSocketFactory.getSocketFactory())

.register("https", SSLConnectionSocketFactory.getSocketFactory()).build());

HTTP_CLIENT_CONNECTION_MANAGER.setDefaultMaxPerRoute(100);

HTTP_CLIENT_CONNECTION_MANAGER.setMaxTotal(200);

RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(60000).setConnectTimeout(60000)

.setSocketTimeout(60000).build();

HTTP_CLIENT = HttpClientBuilder.create().setConnectionManager(HTTP_CLIENT_CONNECTION_MANAGER)

.setDefaultRequestConfig(requestConfig).build();

}

HttpGet httpGet = new HttpGet(url + (StringUtils.contains(url, "?") ? "&" : "?")

+ EntityUtils.toString(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")));

CloseableHttpResponse httpResponse = HTTP_CLIENT.execute(httpGet);

----------------------------------------------------------------------------------------------------------------

解决方法:加上SSL的验证,加上协议

修改后可用的代码:

private static final PoolingHttpClientConnectionManager HTTP_CLIENT_CONNECTION_MANAGER;

private static final CloseableHttpClient HTTP_CLIENT;

static {

SSLContext ctx = SSLContexts.createSystemDefault();

SSLConnectionSocketFactory fac =

new SSLConnectionSocketFactory(ctx, new String[]{"SSLv2Hello", "TLSv1.2"}, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

HTTP_CLIENT_CONNECTION_MANAGER = new PoolingHttpClientConnectionManager(RegistryBuilder

. create().register("http", PlainConnectionSocketFactory.getSocketFactory())

.register("https", fac).build());

HTTP_CLIENT_CONNECTION_MANAGER.setDefaultMaxPerRoute(100);

HTTP_CLIENT_CONNECTION_MANAGER.setMaxTotal(200);

RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(60000).setConnectTimeout(60000)

.setSocketTimeout(60000).build();

HTTP_CLIENT = HttpClientBuilder.create().setConnectionManager(HTTP_CLIENT_CONNECTION_MANAGER)

.setDefaultRequestConfig(requestConfig).build();

}

HttpGet httpGet = new HttpGet(url + (StringUtils.contains(url, "?") ? "&" : "?")

+ EntityUtils.toString(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")));

CloseableHttpResponse httpResponse = HTTP_CLIENT.execute(httpGet);

希望能帮到你。。。

转载地址:http://alggp.baihongyu.com/

你可能感兴趣的文章
Maven 编译
查看>>
《学习之道》第十章学习方法29还记得散步的好处嘛
查看>>
Git常用命令总结
查看>>
iOS获取设备IP地址
查看>>
JavaSE| String常用方法
查看>>
NRF51822配对绑定要点
查看>>
C语言博客作业—数据类型
查看>>
Python封装与隐藏
查看>>
[leetcode]Count and Say
查看>>
cookie、session和token的概念入门
查看>>
保护网站页面内容+版权
查看>>
Golang模拟客户端POST表单功能文件上传
查看>>
重启进程
查看>>
js 进度条效果
查看>>
RelativeLayout
查看>>
2014 10 07 ················男人感悟100(转自MOP)
查看>>
安卓开发之生成cache目录和files目录
查看>>
LeetCode232:Implement Queue using Stacks
查看>>
安装eclipse maven插件m2eclipse No repository found containing
查看>>
配置 Docker 镜像下载的本地 mirror 服务
查看>>