Our business code in the need to crawl the product image address, and then go to our server process, in the middle of course, the process of downloading pictures, the recent target site some changes, that is, the previous HTTP prefix image address part of the change to HTTPS, and then caused a problem, the picture can not be downloaded, Look at log and discover a Java sslexception.
Exception is: java.lang.RuntimeException:Received fatal alert:protocol_version
The root cause of the anomaly is: javax.net.ssl.SSLException:Received fatal alert:protocol_version.
--------------------------------------------------------------------------------------------------------------- -
The reason is: HTTPS requests need to add SSL authentication, but the code is generally only used in HTTP, even if the use of HTTPS is only a change of prefix, and did not carry the process of SSL authentication.
Source:
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);
--------------------------------------------------------------------------------------------------------------- -
Workaround: Add SSL authentication, plus protocol
Code that is available after modification:
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);
Hope to help you ...
How to solve the problem of https Java sslexception protocol_version