HarmonyOS 鸿蒙Next 怎么发送http请求

发布于 1周前 作者 bupafengyu 来自 鸿蒙OS

HarmonyOS 鸿蒙Next 怎么发送http请求

web 怎么发送http://请求, 不是https
现在我打开http://image/data/storage/el2/base/haps/entry/cache/xxx.png
一直报错:This request has been blocked; the content must be served over HTTPS.

2 回复
设置MixedMode为MixedMode.All和domStorageAccess为true

在HarmonyOS(鸿蒙)系统中发送HTTP请求,通常可以通过集成网络请求库或使用原生API来实现。以下是一个基于Java或Kotlin(适用于鸿蒙应用开发)的基本示例,展示了如何发送HTTP GET请求:

  1. 引入网络权限:在config.json文件中添加网络权限,例如:

    "module": {
      "package": "your.package.name",
      "reqPermissions": [
        "ohos.permission.INTERNET"
      ]
    }
    
  2. 使用HttpURLConnection发送请求

    import ohos.aafwk.ability.Ability;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class MainAbility extends Ability {
        public void sendGetRequest(String urlString) {
            try {
                URL url = new URL(urlString);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                // 处理响应
                System.out.println(response.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部