首页 > 解决方案 > 不安全的 HTTP 在 Flutter 2.0.0 及更高版本中不起作用

问题描述

我正在使用flutter 2.0.0并降级它,flutter 2.2.3但仍然无法解决问题。

我已经关注以下问题:

当然还有这里的官方文档。

因此,我正在尝试使用http://my-ip:5000 之类的 URL访问我的Azure VM上的本地主机。我已经完成了以下步骤,但仍然无法访问它。

注意:当我用 POSTMAN 测试它时它工作正常

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cui.adam">
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    

   <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="ADAM"
        android:usesCleartextTraffic="true"
        android:icon="@mipmap/ic_launcher">
        <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
        </receiver>
        <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
              <action android:name="FLUTTER_NOTIFICATION_CLICK" />
              <category android:name="android.intent.category.DEFAULT" />
            </intent-filter> 
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <meta-data android:name="io.flutter.network-policy"
              android:resource="@xml/network_security_config"/>
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

network_system_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

颤振代码

CustomButton(
  btnWidth: 100.0,
  btnHeight: 45.0,
  btnOnPressed: () async {
    if (_formKey.currentState.validate()) {
      setState(() {
        _isWorking = true;
      });
      String url =
          "http://my-IP:5000/end-point";

      var body = {
        "username": "someUsername",
        "password": "somePassword",
        "targeted_username": "someUsername",
      };
      var headers = {
        "Accept": "application/json",
        "Access-Control-Allow-Origin": "*"
      };
      http.Response response = await http
          .post(
        Uri.parse(url),
        body: body,
        headers: headers,
      )
          .whenComplete(() {
        setState(() {
          _isWorking = false;
        });
      });
      print(response.body);
      if (response.statusCode == 200) {
        setState(() {
          _dataScraped = !_dataScraped;
        });
        print(response.body);
      } else {
        print("SOME ERROR!!");
      }
    }
  },
  btnColor: kLightGreenColor,
  btnText: _isWorking
      ? kLoaderWhite
      : Text(
          "Get Data",
          style: kBtnTextStyle,
        ),
),

扑医生

[√] Flutter (Channel beta, 2.0.0, on Microsoft Windows [Version 10.0.19043.1165], locale en-US)
    • Flutter version 2.0.0 at D:\flutter2
    • Framework revision 60bd88df91 (6 months ago), 2021-03-03 09:13:17 -0800
    • Engine revision 40441def69
    • Dart version 2.12.0

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    • Android SDK at C:\Users\Hamza\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.3
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 4.1.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] VS Code (version 1.59.1)
    • VS Code at C:\Users\Hamza\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.25.0

[√] Connected device (3 available)
    • CPH1909 (mobile) • RGMFGAFIYDW8AQ5S • android-arm64  • Android 8.1.0 (API 27)
    • Chrome (web)     • chrome           • web-javascript • Google Chrome 92.0.4515.159
    • Edge (web)       • edge             • web-javascript • Microsoft Edge 92.0.902.84

• No issues found!

标签: androidazureflutterhttp

解决方案


您已经创建了网络配置,但没有在Manifest! 像这样添加,

<application
  android:networkSecurityConfig="@xml/network_system_config">
  
  <!--Other Stuff In Between-->

</application>

删除cleartextTrafficPermittedManifest因为您已经在网络配置中拥有它。

如果这些不起作用,那么<domain>像这样添加到您的网络配置中,

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">http://mywebsite.xyz</domain>
        <domain includeSubdomains="true">http://mydatabaseurl.abc</domain>
    </domain-config>
</network-security-config>

推荐阅读