鸿蒙学习记录之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();
    }
  }
);
相关推荐
手揽回忆怎么睡3 分钟前
Streamlit学习实战教程级,一个交互式的机器学习实验平台!
人工智能·学习·机器学习
xiaoxiaoxiaolll6 分钟前
《Advanced Materials》基于MXene的复合纤维实现智能纺织品多模态功能集成
学习
only火车头20 分钟前
升级 ceph (16.2 -> 18.2) ceph mon 启动失败
服务器·ceph
RisunJan39 分钟前
Linux命令-ifconfig命令(配置和显示网络接口的信息)
linux·运维·服务器
lbb 小魔仙1 小时前
【Linux】100 天 Linux 入门:从命令行到 Shell 脚本,告别“光标恐惧”
linux·运维·服务器
db_murphy1 小时前
学习篇 | 英方i2Active和i2Stream工具了解
学习
早川9192 小时前
Linux系统
linux·运维·服务器
强子感冒了2 小时前
Java学习笔记:String、StringBuilder与StringBuffer
java·开发语言·笔记·学习
BullSmall2 小时前
Doris的备份及恢复方案
学习
小李子不吃李子2 小时前
人工智能与创新第二章练习题
人工智能·学习