httpclient trust all certificates resolve SSLException: Unrecognized SSL message, plaintext connection

HttpClient error when using a third-party tool call Http Interface  javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection ?

This error means that information does not recognize SSL, plaintext connection?

This means that the look does not recognize SSL information when accessing network resources using the https protocol.

SSL (Secure Socket Layer Secure Sockets Layer) encryption is based on a protocol layer below HTTPS, originally developed by Netscape (Netscape) research and development, after the IETF (The Internet Engineering Task Force - the Internet Engineering Task Force) is written after standardization ( RFCRequest For comments request for comments), RFC specification contains a lot of Internet technology!

At first because HTTP is used when data is transmitted in clear text (on the report when the body can not see Although the data submitted by the POST, but still can steal through packet capture tool) is not safe, in order to solve the hidden dangers Netscape introduced SSL secure sockets layer, SSL is a protocol layer below HTTP over TCP, HTTP is a standard and when encrypted data based on the TCP transport, it is HPPTS HTTP + SSL / TCP abbreviation .

SSL protocol is located between the TCP / IP protocol with a variety of application-layer protocol that provides secure support for data communications. SSL protocol can be divided into two layers: SSL Record Protocol (SSL Record Protocol): it is based on a reliable transport protocol (e.g., TCP), to provide high-level protocol data encapsulation, compression, encryption support basic functions. SSL handshake protocol (SSL Handshake Protocol): It is built on top of the SSL Record protocol for data transmission before the actual start of communication between the two sides authentication, negotiate an encryption algorithm, encryption key exchange and so on.

 Originally https is encrypted on the basis of http. Use SSL encryption protocol.

Both sides of this communication is necessary to do identity verification prior to communication, by way of verifying the identity of the certificate.

The original certificate is a problem area, we need to add the code to make it all trust certificates.

The following code, set up trust all agents.

import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
importjava.security.cert.CertificateException;
 Import java.security.cert.X509Certificate; 

public  class HttpClientUtil { 
    
    public  static CloseableHttpClient createSSLClientDefault () {
         the try {
             // use loadTrustMaterial () method to implement a trust policy, trust all certificates 
            SSLContext SSLContext = new new SSLContextBuilder () .loadTrustMaterial ( null , new new TrustStrategy () {
                 // trust all 
                public  boolean IsTrusted (X509Certificate [] catena alberghiera, String authType) throws CertificateException {
                     return to true ; 
                } 
            .}) Build (); 
            // NoopHostnameVerifier categories: verification tool as the host name, host name verification is substantially closed, it can take any
             // valid SSL session and matched to the target host. 
            = HostnameVerifier the HostnameVerifier NoopHostnameVerifier.INSTANCE; 
            SSLConnectionSocketFactory sslsf = new new SSLConnectionSocketFactory (SSLContext, hostnameVerifier);
             return HttpClients.custom () setSSLSocketFactory (sslsf) .build ();. 
        } The catch (KeyManagementException E) { 
            e.printStackTrace (); 
        } the catch  ( NoSuchAlgorithmException e) {
            E .printStackTrace (); 
        }catch (KeyStoreException e) {
            e.printStackTrace();
        }
        return HttpClients.createDefault();

    }
}

 

 Get HttpClient way of example by the use of the default instance httpclient become our custom httpclient

That is, by the

 

Changes to

This solves my problem.

Guess you like

Origin www.cnblogs.com/ibigboy/p/11265855.html