不安全的 HTTP 在 Flutter 2.0.0 及更高版本中不起作用

Posted

技术标签:

【中文标题】不安全的 HTTP 在 Flutter 2.0.0 及更高版本中不起作用【英文标题】:Insecure HTTP not working in Flutter 2.0.0 and above 【发布时间】:2021-10-30 22:16:32 【问题描述】:

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

我已经关注了以下问题:

Flutter Insecure http is not allowed by platform Bad state: Insecure HTTP is not allowed by platform: How to solve insecure http not allowed by platform flutter? Insecure HTTP is not allowed by platform

当然还有官方文档here。

所以,我正在尝试使用 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!

【问题讨论】:

【参考方案1】:

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

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

</application>

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

如果这些不起作用,请像这样将&lt;domain&gt; 添加到您的网络配置中,

<?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>

【讨论】:

我完成了上述步骤,但他们只使用GET 而不是POST。同样,GETPOST 与 Postman 一起工作正常

以上是关于不安全的 HTTP 在 Flutter 2.0.0 及更高版本中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

平台不允许 Flutter Insecure http

错误状态:平台不允许使用不安全的 HTTP:http://0.0.0.0:9090

未处理的异常:错误状态:平台不允许使用不安全的 HTTP - usesClearTextTraffic 不起作用

(Flutter) pub 聊天失败取决于 charts_flutter >=0.0.1+1 <0.4.0 这需要 SDK 版本 >=1.23.0 <2.0.0 退出代码 1

2.解析 — Flutter 不同版本之间的区别

Flutter项目插件整理