鸿蒙学习记录之http网络请求

权限申请

路径为:entry -> src -> main -> module.json5

复制代码
"requestPermissions": [
      {
        "name": "ohos.permission.INTERNET",
      }
    ]

基本使用

复制代码
// 1. 引入包名
import { http } from '@kit.NetworkKit';

// 2. 创建一个HttpRequest对象。
let httpRequest = http.createHttp();

// 3. 调用request()方法。
httpRequest.request(
  "url",
  {
    method: http.RequestMethod.POST, // 默认为http.RequestMethod.GET
    header: [{
      'Content-Type': 'application/json'
    }],
    // 一般情况下:post、put等请求传入对象,get请求传`key=value`形式字符串
    extraData: "data to send",
  }, (err: BusinessError, data: http.HttpResponse) => {
    if (!err) {
      // 4. 解析返回结果。
      console.info('Result:' + JSON.stringify(data.result));
      console.info('code:' + JSON.stringify(data.responseCode));
      // 5. 调用destroy方法销毁
      httpRequest.destroy();
    } else {
      console.error('error:' + JSON.stringify(err));
      httpRequest.destroy();
    }
  }
);
相关推荐
迪丽热爱几秒前
多媒体应用5-817
学习
世人万千丶1 分钟前
收纳格卡片风:ArkUI 让鸿蒙物品清单像收纳盒贴标
学习·华为·harmonyos·鸿蒙
予昊6 分钟前
Linux 资源控制实战分析:用 LXC 亲手造一个 “迷你虚拟机“
linux·运维·服务器
MartinYeung510 分钟前
[论文学习]X-Boundary:为LLM建立精确安全边界以抵御多轮越狱攻击
学习·安全
xiaoxiangsiyan16 分钟前
RHCE之2026年全套考点详解
运维·前端·网络·chrome·学习·rhce
hyf32663344 分钟前
低成本站群泛程序部署:单服务器托管百站稳定运行教程
运维·服务器·前端·搜索引擎·蜘蛛池
爱莉希雅&&&1 小时前
Kubernetes Service 完整学习笔记
笔记·学习·kubernetes
张宇Joaquin1 小时前
openEuler编译和更换内核的方法
java·linux·服务器
小此方1 小时前
Re:Linux系统篇(五十六)线程篇 · 九:基于阻塞队列的生产者消费者模型与条件变量
linux·运维·服务器
Escalating_xu1 小时前
【Linux】库制作与链接原理:从静态库、动态库到 ELF、重定位、GOT 与 PLT
linux·运维·服务器