React-native fetch() from https server with self-signed certificate

前端 未结 7 1230
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 01:39

I\'m trying to communicate with https server having self-signed certificate.

I can do this from .NET application (using ServicePointManager.ServerCertificateValidati

相关标签:
7条回答
  • 2020-11-30 02:37

    I got this working on Android by doing the following:

    1. Install the CA on your device under Settings -> Security & location > Advanced > Encryption & credentials > Install from storage. You can confirm it's installed correctly by visiting the domain through a web browser on your device. If the certificate validates, then the CA is installed.
    2. Create a network security configuration at res/xml/network_security_config.xml with the following contents. Update the domain(s).
    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
            <!-- For React Native Hot-reloading system -->
            <!-- If you are running on a device insert your computer IP -->
            <domain includeSubdomains="true">localhost</domain>
            <domain includeSubdomains="true">your self signed domain</domain>
    
            <trust-anchors>
                <certificates src="system" />
                <certificates src="user" />
            </trust-anchors>
        </domain-config>
    
        <base-config cleartextTrafficPermitted="false" />
    </network-security-config>
    
    1. Reference this configuration file from your AndroidManifest.xml.
    <?xml version="1.0" encoding="utf-8"?>
    <manifest ... >
        <application android:networkSecurityConfig="@xml/network_security_config"
                        ... >
            ...
        </application>
    </manifest>
    
    0 讨论(0)
提交回复
热议问题