capacitor-community / http

Community plugin for native HTTP
MIT License
209 stars 136 forks source link

Request to https domain fails with SSLHandshakeException. #260

Open Petru-design opened 2 years ago

Petru-design commented 2 years ago

Request to https domain fails with SSLHandshakeException.

I am trying to perform http requests to my backend api server, which has a valid certificate and is running https. Unfortunately, the request to my particular domain fails, as can be seen in the screenshot attached. Is this an issue with the certificate? I have been reading something about SSL pinning, but not sure that means and how to do it

Expected behavior A clear and concise description of what you expected to happen.

Screenshots image

Smartphone (please complete the following information):

Rishabprasad commented 2 years ago

You need to add networkconfig.xml file in android folder. Networkconfig.xml look like this: <?xml version="1.0" encoding="UTF-8"?>

youneedtoadddomain/hostnameappwantstoreach youneedtoadddomain/hostnameappwantstoreach

Path to add that file: C:\Users\\Documents\Project\\android\app\src\main\res\xml\network_security_config.xml Do not forgot to add below line in manifest.xml file; C:\Users\\Documents\Project\\android\app\src\main\AndroidManifest.xml <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:networkSecurityConfig="@xml/network_security_config" android:theme="@style/AppTheme">

demoprojects123 commented 2 years ago

@Rishabprasad Can you please write /paste here picture or code related to SSL pinning in code and Android Studio code. Stepwise procedure. [For valid and self-signed SSL certificate] .Using IONIC Framework+React+api calling library:capacitor-community/http. please write headers also if necessary for https api calling. I am stuck. Kindly HELP.

Rishabprasad commented 2 years ago
  1. Create network_security_config.xml on this location: C:\Users\\Documents\Project\xyz\android\app\src\main\res\xml\network_security_config.xml
  2. Add following code into it;
    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">connecttohostserver.com</domain>
        <domain includeSubdomains="true">connecttohostserver.net</domain>
        <trust-anchors>
            <certificates src="system" />
            <certificates src="user" />
        </trust-anchors>
    </domain-config>
    </network-security-config>
  3. Add this xml file reference into C:\Users\xyz\Documents\Project\projectname\android\app\src\main\AndroidManifest.xml
    <?xml version='1.0' encoding='utf-8'?>
    <manifest package="com.project.demo" xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" **android:networkSecurityConfig="@xml/network_security_config"**  android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" **android:usesCleartextTraffic="true"**>
demoprojects123 commented 2 years ago

@Rishabprasad Thanks for Android studio code. Please share SSL Pinning code in React. How to call apis with ssl pinning. Please share code.

demoprojects123 commented 2 years ago

@Rishabprasad Can you paste the pinning code here for reference. as It is not working for self-signed certificate [Internal company] for https url. It is throwing SSLHANDSHAKEEXCEPTION ERROR after including your android studio code. I am using IONIC Framework+ React+ capacitor-community/http for api calling. HELP.!