How to solve the problem of https Java sslexception protocol_version

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.