當RN https 遇見Android 和 http 遇見IOS 的處理方法,androidios

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.