https Java SSLException protocol_version的问题解决方法

Posted 草木有本心,何求美人折!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了https Java SSLException protocol_version的问题解决方法相关的知识,希望对你有一定的参考价值。

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

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

异常的根源是: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
				.<ConnectionSocketFactory> 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
				.<ConnectionSocketFactory> 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);

  

希望能帮到你。。。

 

以上是关于https Java SSLException protocol_version的问题解决方法的主要内容,如果未能解决你的问题,请参考以下文章

带有 FTP 登录错误的 HTTPS 客户端:javax.net.ssl.SSLException: 502 SSLv23/TLSv1 java

HttpClient SSLException

Java SSLException:证书中的主机名不匹配

javax.net.ssl.SSLException:服务器选择了不正确的密码套件 SSL_RSA_WITH_DES_CBC_SHA

Java8、JBoss AS5、SSLException:“收到致命警报:handshake_failure”

httpclient信任所有证书解决SSLException:Unrecognized SSL message,plaintext connection