1

我将 OkHttpClient 库用于 HTTP 请求,并在“res/raw”文件夹中有两个“.bks”文件用于进行 SSL 固定。我编写了以下代码来获取一个“.bks”文件。但是如何添加多个文件?

  • dkhymnow.bks

  • dpsandroid.bks

我希望有人能帮助我!

private OkHttpClient getOkHttpClient() {
    try {
      OkHttpClient.Builder builder = new OkHttpClient.Builder();
      builder.sslSocketFactory(getPinnedCertSslSocketFactory(context));
      builder.connectTimeout(30, TimeUnit.SECONDS);
      builder.writeTimeout(30, TimeUnit.SECONDS);
      builder.readTimeout(30, TimeUnit.SECONDS);

      OkHttpClient okHttpClient = builder.build();
      return okHttpClient;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  private SSLSocketFactory getPinnedCertSslSocketFactory(Context context) {
    try {
      KeyStore trusted = KeyStore.getInstance("BKS");
      InputStream in = context.getResources().openRawResource(R.raw.dkhymnow);
      trusted.load(in, STORE_PASS);
      SSLContext sslContext = SSLContext.getInstance("TLS");
      TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
        TrustManagerFactory.getDefaultAlgorithm());
      trustManagerFactory.init(trusted);
      sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
      return sslContext.getSocketFactory();
    } catch (Exception e) {
      Log.e("MyApp", e.getMessage(), e);
    }
    return null;
  }
4

0 回答 0