定义 SSLSocketFactoryCompat,在创建 Socket 的时候如果是 4.x 的设备则启用 TLSv1.2 import android.os.Build; import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; public class SSLSocketFactoryCompat extends SSLSocketFactory{ private static final String[] TLS_V12_ONLY = {"TLSv1.2"}; private final SSLSocketFactory delegate; public SSLSocketFactoryCompat() throws KeyManagementException, NoSuchAlgorithmException { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, null, null); delegate = sc.getSocketFactory(); } public SSLSocketFactoryCompat(SSLSocketFactory delegate) { if (delegate == null) { throw new NullPointerException(); } this.delegate = delegate; } @Override public String[] getDefaultCipherSuites() { return delegate.getDefaultCipherSuites(); } @Override public String[] getSupportedCipherSuites() { return delegate.getSupportedCipherSuites(); } private Socket enableTls12(Socket socket) { if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT < 20)……

阅读全文