使用http模块实现数据流请求

根据URL地址,发起HTTP网络请求并返回流式响应

HarmonyOS
2024-05-28 20:36:03
1.2w浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
人提唱盘

使用的核心API

http模块数据流传递能力:@ohos.http.requestInstream

核心代码解释

 //使用数据流形式请求传输网络数据 
streamHttpRequest(url: string) { 
    let httpRequest = http.createHttp(); 
    httpRequest.on("dataReceive", (data: ArrayBuffer) => { 
      console.info("dataReceive length: " + JSON.stringify(data.byteLength)); 
    }); 
    httpRequest.on("dataEnd", () => { 
      console.info("Receive dataEnd !"); 
    }); 
    httpRequest.on("dataReceiveProgress", (data: RequestData) => { 
      console.info("dataReceiveProgress:" + JSON.stringify(data)); 
    }); 
  
    httpRequest.requestInStream(url, (err: BusinessError, data: number) => { 
      if (!err) { 
        console.info("requestInStream OK! ResponseCode is " + JSON.stringify(data)); 
      } else { 
        console.info("requestInStream ERROR : err = " + JSON.stringify(err)); 
      } 
    }) 
  
  
    httpRequest.off("dataReceiveProgress"); 
    httpRequest.off("dataEnd"); 
    httpRequest.off("dataReceive"); 
  
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

注明适配的版本信息

IDE版本:4.1.3.500

SDK版本:HarmonyOS Next Developer Preview0

分享
微博
QQ
微信
回复
2024-05-29 21:41:04


相关问题
HarmonyOS http请求数据报错
727浏览 • 1回复 待解决
HarmonyOS 如何解压数据流
787浏览 • 1回复 待解决
实现文件解压缩和数据流解压缩
1885浏览 • 1回复 待解决
HarmonyOS 解压gzip格式压缩的数据流
432浏览 • 1回复 待解决
http request 请求不到接口数据
5571浏览 • 1回复 待解决
websocket和http数据请求示例
1382浏览 • 1回复 待解决
使用http请求网络地址
1361浏览 • 1回复 待解决
http请求证书校验实现
963浏览 • 1回复 待解决
HTTP请求使用同一SESSIONID
1081浏览 • 1回复 待解决
HarmonyOS http请求的JSON数据定义问题
790浏览 • 1回复 待解决