鸿蒙学习记录之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();
    }
  }
);
相关推荐
Xの哲學11 分钟前
Linux多级时间轮:高精度定时器的艺术与科学
linux·服务器·网络·算法·边缘计算
FlourishingMind13 分钟前
蓝牙授时CTS (Current Time Service)、PTP、NTP
运维·服务器·网络
蓝桉~MLGT18 分钟前
Ai-Agent学习历程—— 阶段1——环境的选择、Pydantic基座、Jupyter Notebook的使用
人工智能·学习·jupyter
QT 小鲜肉1 小时前
【Linux命令大全】001.文件管理之mmove命令(实操篇)
linux·服务器·前端·chrome·笔记
MaximusCoder1 小时前
Linux信息收集Command
运维·服务器·经验分享
QT 小鲜肉1 小时前
【Linux命令大全】001.文件管理之mdel命令(实操篇)
linux·运维·服务器·chrome·笔记
杰米不放弃2 小时前
AI大模型应用开发学习-26【20251227】
人工智能·学习
就不掉头发2 小时前
I/O复用
运维·服务器·c语言·开发语言
2401_832298102 小时前
云服务器:边缘计算时代的“智能节点”
运维·服务器·边缘计算
执笔论英雄2 小时前
【RL】Megatron使学习forward_backward_func返回值
学习