HarmonyOS鸿蒙Next中API18版本应用应该如何配置才可以发起Http请求,而不是非要Https请求呢?

HarmonyOS鸿蒙Next中API18版本应用应该如何配置才可以发起Http请求,而不是非要Https请求呢? 情景: 我在本地启动Java项目, 想进行调试 , 结果我的鸿蒙应用无法请求到我的Java服务器,我本地的访问地址是http的 , 访问其他网站的https请求是能访问到的。请问我该如何配置才能发起http请求

https://developer.harmonyos.com/cn/docs/documentation/doc-guides/package-structure-0000001333321033这个帖子介绍的方案已经过时了,已经没有config.json文件了。


更多关于HarmonyOS鸿蒙Next中API18版本应用应该如何配置才可以发起Http请求,而不是非要Https请求呢?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在HarmonyOS API18中配置HTTP请求需在config.json中修改网络安全策略。具体步骤:

  1. 打开entry/src/main/resources/base/profile目录下的config.json文件

  2. "module"对象中添加:

    "reqPermissions": [
        {
            "name": "ohos.permission.INTERNET",
            "reason": "网络请求权限"
        },
        {
            "name": "ohos.permission.MODIFY_NETWORK_STATE"
        }
    ],
    "network": {
        "cleartextTraffic": true
    }
    
  3. 保存后重新编译应用,

更多关于HarmonyOS鸿蒙Next中API18版本应用应该如何配置才可以发起Http请求,而不是非要Https请求呢?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next API18中,要允许应用发起HTTP请求,需要在应用的配置文件中进行设置。由于API18已改用module.json5替代旧的config.json,请按照以下步骤配置:

  1. 打开项目中的module.json5文件
  2. 在"module"对象下添加"metadata"配置项:
"metadata": [
  {
    "name": "default.network.security.config",
    "value": "@xml/network_config"
  }
]
  1. 在resources/base/xml目录下创建network_config.xml文件(如不存在该目录需手动创建)
  2. 在network_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>

这样配置后,应用就能发起HTTP请求了。注意这仅适用于开发调试环境,正式发布时应使用HTTPS以确保安全性。

回到顶部