Android Https证书过期的两种解决方案

  private static OkHttpClient newOkHttpClient(int timeout){

  HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

  logging.setLevel(HttpLoggingInterceptor.Level.BODY);

  return new OkHttpClient.Builder()

  .addInterceptor(new RequestInfoInterceptor())

  //.addInterceptor(logging)

  .addNetworkInterceptor(new TokenHeaderInterceptor())

  .sslSocketFactory(Certificate.getSSLSocketFactory(BaseApplcation.myApp, new String[]{"/sdcard/xxx.cer"}))

  .hostnameVerifier(Certificate.getUnSafeHostnameVerifier())

  .readTimeout(timeout, TimeUnit.SECONDS)

  .writeTimeout(timeout, TimeUnit.SECONDS)

  .build();

  }

  /**

  * 带证书的,从本地文件读取

  * @param context

  * @param certificatesFiles 本地文件(通过下载到本地)

  * @return

  */

  public static SSLSocketFactory getSSLSocketFactory(Context context, String[] certificatesFiles) {

  if (context == null) {

  throw new NullPointerException("context == null");

  }

  CertificateFactory certificateFactory;

  try {

  certificateFactory = CertificateFactory.getInstance("X.509");

  KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());

  keyStore.load(null, null);

  for (int i = 0; i < certificatesFiles.length; i++) {

  InputStream certificate = new FileInputStream(certificatesFiles[i]);

  keyStore.setCertificateEntry(String.valueOf(i), certificateFactory.generateCertificate(certificate));

  if (certificate != null) {

  certificate.close();

  }

  }

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

  TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());

  trustManagerFactory.init(keyStore);

  sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());

  return sslContext.getSocketFactory();

  } catch (Exception e) {

  }

  return null;

  }

  /**

  * 带证书的,从raw资源中读取

  * @param context

  * @param certificates rawIds

  * @return

  */

  public static SSLSocketFactory getSSLSocketFactory(Context context, int[] certificates) {

  if (context == null) {

  throw new NullPointerException("context == null");

  }

  CertificateFactory certificateFactory;

  try {

  certificateFactory = CertificateFactory.getInstance("X.509");

  KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());

  keyStore.load(null, null);

  for (int i = 0; i < certificates.length; i++) {

  InputStream certificate = context.getResources().openRawResource(certificates[i]);

  keyStore.setCertificateEntry(String.valueOf(i), certificateFactory.generateCertificate(certificate));

  if (certificate != null) {

  certificate.close();

  }

  }

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

  TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());

  trustManagerFactory.init(keyStore);

  sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());

  return sslContext.getSocketFactory();

  } catch (Exception e) {

  }

  return null;

  }