zoukankan      html  css  js  c++  java
  • 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);

      

    希望能帮到你。。。

  • 相关阅读:
    安德鲁斯称三步系统相机
    Swift游戏开发实战教程(霸内部信息大学)
    ORA-00911:无效字符错误
    Java对多线程~~~Fork/Join同步和异步帧
    jquery跨域请求解决方案(我们寻找,我还没有添加验证)
    mysql 的load data infile要使用
    完整详细的说明GCD列(一)dispatch_async;dispatch_sync;dispatch_async_f;dispatch_sync_f
    Linux查看非root流程执行
    Android_显示器本身被卸载应用程序
    关于微软公有云Azure会计标准
  • 原文地址:https://www.cnblogs.com/tonyccc/p/9468307.html
Copyright © 2011-2022 走看看