package nc.hrhi.httpUtil;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Arrays;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.util.TextUtils;

import okhttp3.OkHttpClient;

//https://www.cnblogs.com/chinaifae/articles/10442797.html
public class SSLSocketClient {

    //获取这个SSLSocketFactory
    public static SSLSocketFactory getSSLSocketFactory() {
        try {
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, getTrustManager(), new SecureRandom());
            return sslContext.getSocketFactory();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //获取TrustManager
    private static TrustManager[] getTrustManager() {
        TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public void checkServerTrusted(X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[]{};
                    }
                }
        };
        return trustAllCerts;
    }

    //获取HostnameVerifier
    public static HostnameVerifier getHostnameVerifier() {
        HostnameVerifier hostnameVerifier = new HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        };
        return hostnameVerifier;
    }
    
    /**

	* okHttp3添加信任所有证书
	 * @return 
	 * @return 

	* @return

	*/

	public static OkHttpClient OkHttpClientgetUnsafeOkHttpClient() {

	try {

		final TrustManager[] trustAllCerts =new TrustManager[]{
			new X509TrustManager() {
	
				@Override
				public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {}
		
				@Override
				public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {}
		
				@Override
				public java.security.cert.X509Certificate[]getAcceptedIssuers() {
					return new java.security.cert.X509Certificate[]{};
				}
			}
		};

		final SSLContext sslContext = SSLContext.getInstance("TLS");

		sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

		final javax.net.ssl.SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

		OkHttpClient.Builder builder =new OkHttpClient.Builder();


		builder.sslSocketFactory(sslSocketFactory,(X509TrustManager)trustAllCerts[0]);

		builder.hostnameVerifier(new HostnameVerifier() {
		String[]arr =new String[]{};
		
		@Override
		public boolean verify(String hostname, SSLSession session) {

		if (TextUtils.isEmpty(hostname)) {
	
			return false;
	
		}

		return !Arrays.asList(arr).contains(hostname);
	
			}
	
		});
			return builder.build();
		}catch (Exception e){
			throw new RuntimeException(e);
		}
	}
}