鸿蒙学习记录之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();
    }
  }
);
相关推荐
茶杯梦轩2 天前
从零起步学习RabbitMQ || 第二章:RabbitMQ 深入理解概念 Producer、Consumer、Exchange、Queue 与企业实战案例
服务器·后端·消息队列
YuMiao4 天前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
不可能的是5 天前
前端 SSE 流式请求三种实现方案全解析
前端·http
Sinclair7 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
Rockbean8 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
茶杯梦轩8 天前
CompletableFuture 在 项目实战 中 创建异步任务 的核心优势及使用场景
服务器·后端·面试
海天鹰9 天前
【免费】PHP主机=域名+解析+主机
服务器
西岸行者9 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
不是二师兄的八戒9 天前
Linux服务器挂载OSS存储的完整实践指南
linux·运维·服务器
芝士雪豹只抽瑞克五9 天前
Nginx 高性能Web服务器笔记
服务器·nginx