Инициализация java класса org.apache.http.client.HttpClient для работы с https

Ниже привожу пример инициализации HttpClient для https доверяющего всем сертификатам

import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.BasicClientConnectionManager;

import java.security.NoSuchAlgorithmException;
import java.security.KeyManagementException;
import java.security.cert.X509Certificate; 
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.SSLContext; 
import javax.net.ssl.HostnameVerifier;


    
public MyHttpClient
{
    .......

    private HttpClient getHttpClient(boolean isHttps)
    {
        if(isHttps)
        {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[] { 
                new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() { 
                    return new X509Certificate[0]; 
                }
                
                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {}
                
                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {}
            }};

            SSLContext sslContext;
            try {
                sslContext = SSLContext.getInstance("SSL");
            } catch (NoSuchAlgorithmException e) { //  if no Provider supports a TrustManagerFactorySpi implementation for the specified protocol.
                System.out.println(e.getMessage());
                return null;
            }
            
            try {
                sslContext.init(null, trustAllCerts, new SecureRandom());
            } catch (KeyManagementException e) {
                System.out.println(e.getMessage());
                return null;
            }
            
            HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
            SSLSocketFactory sf = new SSLSocketFactory(sslContext, (X509HostnameVerifier)hostnameVerifier);
            
            Scheme httpsScheme = new Scheme("https", 443, sf);
            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(httpsScheme);

            ClientConnectionManager cm = new BasicClientConnectionManager(schemeRegistry);
            return new DefaultHttpClient(cm);
        } 
        else
        {
            return new DefaultHttpClient();
        }    
    }        
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>