忽略Android React Native上的SSL证书检查

Rea*_*Man 12 javascript ssl networking android react-native

我目前正在使用Android上的本机做出反应.我正在使用fetch()发出api请求,但请求给我一个网络请求失败,这是由于端点没有ssl证书.我能够通过修改一些xcode文件来删除iOS上的这项检查.有没有办法忽略Android上的ssl证书检查?

Jac*_*bGC 2

/**
 * Disables the SSL certificate checking for new instances of {@link HttpsURLConnection} This has been created to
 * aid testing on a local box, not for use on production.
 */
private static void disableSSLCertificateChecking() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // Not implemented
        }

        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            // Not implemented
        }
    } };

    try {
        SSLContext sc = SSLContext.getInstance("TLS");

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

        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里找到这个: https: //gist.github.com/aembleton/889392。不太确定它是否有效,但它是一个开始!

  • 嗨女巫文档我应该粘贴它吗? (2认同)