當RN https 遇見Android 和 http 遇見IOS 的處理方法,androidios
1 IOS 的請求和載入http的處理
注意:
NSAppTransportSecurity
NSAllowsArbitraryLoads
2.當android 遇到https;
由於RectNative 底層請求網路用的OKhttp預設是無法載入https 的請求
處理稍微複雜:需要修改Android 原生代碼處理所有https的協議
package com.wx_hcx_rn;import android.app.Application;import com.facebook.react.ReactApplication;import com.facebook.react.ReactNativeHost;import com.facebook.react.ReactPackage;import com.facebook.react.modules.network.OkHttpClientProvider;import com.facebook.react.shell.MainReactPackage;import com.facebook.soloader.SoLoader;import java.util.Arrays;import java.util.List;public class MainApplication extends Application implements ReactApplication { private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { @Override public boolean getUseDeveloperSupport() { return BuildConfig.DEBUG; } @Override protected List getPackages() { return Arrays.asList( new MainReactPackage() ); } @Override protected String getJSMainModuleName() { return "index"; } }; @Override public ReactNativeHost getReactNativeHost() { return mReactNativeHost; } @Override public void onCreate() { super.onCreate();//修改okhttpclient OkHttpClientProvider.replaceOkHttpClient(HttpsOkHttpClient.initCustomOkHttpClient()); SoLoader.init(this, /* native exopackage */ false); }}
下面是自訂的okhttpclient
package com.wx_hcx_rn;import com.facebook.react.modules.network.OkHttpClientProvider;import com.facebook.react.modules.network.ReactCookieJarContainer;import java.security.SecureRandom;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.concurrent.TimeUnit;import javax.net.ssl.HostnameVerifier;import javax.net.ssl.KeyManager;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 okhttp3.OkHttpClient;/** * .::::. * .::::::::. * ::::::::::: FUCK YOU * ..:::::::::::' * '::::::::::::' * .:::::::::: * '::::::::::::::.. * ..::::::::::::. * ``:::::::::::::::: * ::::``:::::::::' .:::. * ::::' ':::::' .::::::::. * .::::' :::: .:::::::'::::. * .:::' ::::: .:::::::::' ':::::. * .::' :::::.:::::::::' ':::::. * .::' ::::::::::::::' ``::::. * …::: ::::::::::::' ``::. * ```` ':. ':::::::::' ::::.. * '.:::::' ':'````.. * * 作者:jiangnanyizhou on 2018/2/6 22:40 * 郵箱:373654981@qq.com */public class HttpsOkHttpClient { /** * react native預設不支援https請求,這裡提供支援https的OKHttpClient * @return */ public static OkHttpClient initCustomOkHttpClient(){ OkHttpClient.Builder client = new OkHttpClient.Builder() .connectTimeout(0, TimeUnit.MILLISECONDS) .readTimeout(0, TimeUnit.MILLISECONDS) .writeTimeout(0, TimeUnit.MILLISECONDS) .cookieJar(new ReactCookieJarContainer()); OkHttpClient.Builder builder = OkHttpClientProvider.enableTls12OnPreLollipop(client); builder.sslSocketFactory(getSSLSocketFactory()) .hostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; //忽略所有的認證,直接返回了true } }); return builder.build(); } private static SSLSocketFactory getSSLSocketFactory() { TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }}; SSLSocketFactory sslSocketFactory = null; try { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init((KeyManager[])null, trustAllCerts, new SecureRandom()); sslSocketFactory = sslContext.getSocketFactory(); } catch (Exception e) { e.printStackTrace(); } return sslSocketFactory; }}