When RN https meets Android and http and IOS, androidios
1. Handling IOS requests and loading http
Note:
NSAppTransportSecurity
NSAllowsArbitraryLoads
2. When android encounters https;
Because the OKhttp of the RectNative underlying request network cannot load https requests by default
Processing is slightly complicated: You need to modify the Android native code to process all https protocols.
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 () {builreturn dconfig. DEBUG;} @ Override protected List
GetPackages () {return Arrays.
AsList (new MainReactPackage () ;}@ Override protected String getJSMainModuleName () {return "index" ;};@ Override public ReactNativeHost getReactNativeHost () {return response ;} @ Override public void onCreate () {super. onCreate (); // modify okhttpclient OkHttpClientProvider. replaceOkHttpClient (HttpsOkHttpClient. initCustomOkHttpClient (); SoLoader. init (this,/* native exopackage */false );}}
The following is the custom 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 *.. :::::::::::'*'::::::::::::'*. ::::::::::*'::::::::::::::.. *.. ::::::::::::. *''::::::::::::::::*::::'':::::::::'. :::. *::::'':::::'. ::::::::. *. ::::': :::. :::::::'::::. *. :::':::::. :::::::::'':::::. *. ::':::::. :::::::::'':::::. *. ::'::::::::::::::'''::::. *... :::::::::::::::'''::. *''''':. ':::::::::'::::.. *'. :::::'':'''''.. ** Author: jiangnanyizhou on * Email: 373654981@qq.com */public class HttpsOkHttpClient {/*** react native does not support https requests by default, 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; // ignore all authentication and return true }}); return builder. build ();} private static SSLSocketFactory getSSLSocketFactory () {TrustManager [] trustAllCerts = new TrustManager [] {new X509TrustManager () {public void checkClientTrusted (pipeline [] chain, String authType) throws CertificateException {} public void checkServerTrusted (LOGIN [] chain, String authType) throws CertificateException {} public login [] 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 ;}}