【问题标题】:Flutter Insecure http is not allowed by platform平台不允许 Flutter Insecure http
【发布时间】:2021-01-18 04:54:02
【问题描述】:

Flutter 团队最近进行了这项更改,现在不允许不安全的 http 连接。 https://flutter.dev/docs/release/breaking-changes/network-policy-ios-android

我想知道如何将移动设备上的 Flutter 应用连接到我的 PC 上运行的本地 go 服务器。

我的服务器正在运行:http://192.168.29.45:4001,但它没有连接到它。

【问题讨论】:

  • 注意阅读如何通过与您的帖子共享的相同指南链接来启用 http 网络请求。
  • 您可以只允许到域的不安全连接。不接受特定的 IP 地址作为输入。这符合平台支持的内容。

标签: android flutter http


【解决方案1】:

将此添加到 android\app\src*debug*\AndroidManifest.xml 文件:

<application android:usesCleartextTraffic="true">
</application>

不要添加到 android\app\src*MAIN*\AndroidManifest.xml 他们是两个不同的东西。 然后停止并重建应用程序,因为热重启和热重载将不起作用,然后所有错误都会消失。

【讨论】:

    【解决方案2】:

    我不知道有人说要更改 app/src/main/AndriodManifest.xml 中的 Android Manifest 文件,但 Flutter webiste 上的教程明确表示:

    如果您想为 Android 调试版本允许 HTTP 连接,您可以将以下 sn-p 添加到您的 $project_path\android\app\src\ debug\AndroidManifest.xml:

    【讨论】:

      【解决方案3】:

      简单的方法:

      安卓

      转到 android/app/src/main/AndroidManifest.xml。打开 AndroidManifest.xml 文件。然后在里面设置 android:usesCleartextTraffic="true"

      例子:

          <uses-permission android:name="android.permission.INTERNET" /> //This line add
          <application
              android:name="io.demo.app.test"
              android:label="ProjectName"
              android:usesCleartextTraffic="true" //This line add
              android:icon="@mipmap/ic_launcher">
      

      iOS

      进入主词典下的ios/Runner/info.plist,打开info.plist设置这行代码:

      <key>NSAppTransportSecurity</key>
      <dict>
          <key>NSAllowsArbitraryLoads</key>
          <true/>
      </dict>
      

      完成这个陡峭之后,关闭这个应用程序并运行:

      • 扑干净
      • 颤振酒吧获取
      • 颤动运行

      【讨论】:

        【解决方案4】:

        我将 Flutter HTTP 库升级到最新版本 https://pub.dev/packages/http

        http: ^0.13.1
        

        然后将 URI 声明更改为 HTTPS 而不是 HTTP

            final http.Response response = await http.post(
                Uri.https('<IP_ADDRESS>', ''),
        
                headers: <String, String>{
                    '<YOUR_HEADERS>',
                },
                body: jsonEncode(<String, Object>{
                    '<REQUEST_BODY>',
                }),
            );
        

        这解决了我的问题

        【讨论】:

        【解决方案5】:

        通常需要(并且更可取)使用 https 链接而不是 http 链接。但是,这可以被覆盖,如下所示。

        安卓

        打开 android/app/src/main 文件夹中的 AndroidManifest.xml 文件。然后将usesCleartextTraffic设置为true

        <application
            ...
            android:usesCleartextTraffic="true"
            ...   >
        

        请参阅this question 了解更多信息。

        iOS

        打开 ios/Runner 文件夹中的 Info.plist 文件。然后添加以下键。

        <key>NSAppTransportSecurity</key>
        <dict>
          <key>NSAllowsArbitraryLoads</key>
              <true/>
        </dict>
        

        请参阅this answer 了解更多信息。

        macOS

        这与 iOS 相同。打开 ma​​cos/Runner 文件夹中的 Info.plist 文件。然后添加以下键。

        <key>NSAppTransportSecurity</key>
        <dict>
          <key>NSAllowsArbitraryLoads</key>
              <true/>
        </dict>
        

        【讨论】:

        • 对于那些在 iOS 上测试并想知道将代码放在Info.plist 中的位置的人;将代码放在&lt;plist&gt;&lt;dict&gt;&lt;/dict&gt;&lt;/plist&gt; 标签之间的iOS 答案中。另外,请仔细检查您的主机防火墙是否未启用。
        【解决方案6】:

        对我来说最简单的解决方案是将以下行添加到我的 DEBUG 清单文件中,该文件可以在 $project_path\android\app\src\debug\AndroidManifest.xml 中找到:

        &lt;application android:usesCleartextTraffic="true"/&gt;

        重要提示 - 直到我重新启动模拟器后它才起作用,因此请确保您不要跳过此步骤。

        【讨论】:

          【解决方案7】:

          如果您已经尝试了上述所有解决方案但他们没有修复它,您应该关闭模拟器并再次打开它,因为 gradle 中的更改是重要的更改,因此它们需要它。需要先关闭模拟器再打开才能看到以上解决方案的效果。

          【讨论】:

            【解决方案8】:

            1- 转到此路径:yourProject\android\app\src\main\res
            并创建名为 xml 的文件夹

            2 - 打开这个路径:yourProject\android\app\src\main\res\xml 并创建名为 network_security_config.xml 的 xml 文件

            3 - 在 network_security_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>
            

            4- 你需要去这个路径:yourProject\android\app\src\main\AndroidManifest.xml:

            <application
                android:usesCleartextTraffic="true"
            
                <activity>
                     <meta-data android:name="io.flutter.network-policy"
                          android:resource="@xml/network_security_config"/>
                 </activity>
             </application>
            

            5- 停止并重新启动您的应用程序,它将与您一起工作

            【讨论】:

            • 你能检查第 4 步的代码吗?我在那边遇到错误Missing 'name' key attribute on element activity at AndroidManifest.xml:46:5-49:17
            【解决方案9】:

            实现这一点的简单方法是在您的 AndroidManifest.xml 中使用此属性,这将允许 http 流量:

            <application android:usesCleartextTraffic="true">
            </application>
            

            More info here

            【讨论】:

              【解决方案10】:

              正如我已经回答的那样。临时解决您的问题。首先在res/xml目录下的network_security_config文件中添加如下配置(我的完整路径是/Users/dolphin/source/third-party/Cruise/android/app/src/main/res/xml/network_security_config):

              <?xml version="1.0" encoding="utf-8"?>
              <network-security-config>
                  <base-config cleartextTrafficPermitted="true" />
              </network-security-config>
              

              然后在您的 AndroidManifest.xml 文件中,在 &lt;application 标签内,添加以下配置:

              android:networkSecurityConfig="@xml/network_security_config"
              

              这可能允许 http 流量。但它只是用于在本地环境中调试或测试环境,最好的方法是使用 https 流量。更多信息:How to allow all Network connection types HTTP and HTTPS in Android (9) Pie?

              【讨论】:

                【解决方案11】:

                我正在使用

                final String baseUrl='http://......herokuapp.com/';

                这个网址给了我这个例外,但后来我做了 httphttps

                final String baseUrl='https://......herokuapp.com/';

                这是出于某些手机的安全目的

                这对我有用,提前谢谢!

                【讨论】:

                  【解决方案12】:

                  在 android/app/AndroidManifest 中

                  <application ...
                    android:networkSecurityConfig="@xml/network_security_config" >
                  
                   <meta-data android:name="io.flutter.network-policy"
                          android:resource="@xml/network_security_config"/>
                  

                  然后创建一个文件夹xml里面创建一个文件:network_security_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>
                  

                  【讨论】:

                    猜你喜欢
                    • 2021-07-30
                    • 2021-08-11
                    • 2021-01-17
                    • 1970-01-01
                    • 2021-01-19
                    • 2020-02-13
                    • 2020-11-29
                    • 2016-01-15
                    • 1970-01-01
                    相关资源
                    最近更新 更多