https Java SSLException protocol_version的問題解決方法

 咱們的業務代碼裏有須要用爬蟲爬取商品的圖片地址,而後在轉到咱們的服務器裏的過程,中間固然少不了下載圖片的過程,最近目標網站有些改變,就是以前http前綴的圖片地址部分改爲了https,而後就形成了一個問題,圖片下載不了,查看log,發現一個java的SSLException.java

異常是: java.lang.RuntimeException:Received fatal alert: protocol_version服務器

異常的根源是:javax.net.ssl.SSLException:  Received fatal alert: protocol_version.網站

----------------------------------------------------------------------------------------------------------------ui

緣由是: https的請求須要加上ssl的驗證,可是代碼裏通常只用到http,即便用到https也只是換了個前綴,並無帶上ssl驗證的過程。url

源代碼:spa

     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, "?") ? "&" : "?").net

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

 

CloseableHttpResponse httpResponse = HTTP_CLIENT.execute(httpGet);orm

  

----------------------------------------------------------------------------------------------------------------blog

 解決方法:加上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);

  

但願能幫到你。。。

相關文章
相關標籤/搜索