When RN https meets Android and http and IOS, androidios

Source: Internet
Author: User

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 ;}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.