Flutter:报错Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform

使用HttpClient进行网络请求,其中请求的url地址是http开头的,flutter 报错 DioError [DioErrorType.DEFAULT]: Bad state: Insecure HTTP is not allowed by platform。

错误解释:平台不支持不安全的 HTTP 协议,即不允许访问 HTTP 域名的地址。

产生原因:IOS 和 Android 9.0 对网络请求做了一些限制,不能直接访问 Http 域名的地址。

解决方案:

iOS:找到Info.plist 文件,修改下列属性:

info.plist

安卓:

1. 为 android 的清单文件 AndroidManifest.xml 的 application 标签内添加两个属性

1.1 AndroidManifest.xml 路径为:android/app/src/main/AndroidManifest.xml) 

1.2 添加的属性为

      android:usesCleartextTraffic="true"

      android:networkSecurityConfig="@xml/network_security_config"

2

2. 添加 network_security_config.xml 文件

2.1 在 android/app/src/main/res 下新建 xml 文件夹

2.2 在 xml 文件夹下创建 network_security_config.xml 文件

2.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>


参考博文:https://blog.csdn.net/weixin_44137575/article/details/109045633

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容