鸿蒙学习记录之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();
    }
  }
);
相关推荐
xuanzdhc2 小时前
Linux 基础IO
linux·运维·服务器
bantinghy3 小时前
Linux进程单例模式运行
linux·服务器·单例模式
静心问道4 小时前
XLSR-Wav2Vec2:用于语音识别的无监督跨语言表示学习
人工智能·学习·语音识别
AWS官方合作商9 天前
AWS ACM 重磅上线:公有 SSL/TLS 证书现可导出,突破 AWS 边界! (突出新功能的重要性和突破性)
服务器·https·ssl·aws
算法练习生9 天前
Linux文件元信息完全指南:权限、链接与时间属性
linux·运维·服务器
忘了ʷºᵇₐ9 天前
Linux系统能ping通ip但无法ping通域名的解决方法
linux·服务器·tcp/ip
weixin_425023009 天前
Spring Boot使用MCP服务器
服务器·spring boot·后端·spring ai·mcp
懒惰的bit9 天前
STM32F103C8T6 学习笔记摘要(四)
笔记·stm32·学习
qq_200337399 天前
python输出值,写入test.yaml文件
java·服务器·前端
成工小白9 天前
【Linux】文件系统
linux·运维·服务器