HarmonyOS httpRequest.on('headersReceive') 返回的header中没有responseCode

按照文档中的方式使用HttpRequest

let httpRequest = http.createHttp();
let arrayBuffers = new Array<ArrayBuffer>();
httpRequest.on('headersReceive', (header: Object) => {
  LogUtil.error('[' + request.requestId +'] loadData' + " headersReceive")
})

httpRequest.on('dataReceive', (data: ArrayBuffer) => {
  // 下载数据流多次返回
  arrayBuffers.push(data);
})

httpRequest.on('dataReceiveProgress', (data: RequestData) => {
  // 下载进度
  if(data != undefined && (typeof data.receiveSize == 'number') && (typeof  data.totalSize == 'number') ) {
    let percent = Math.round(((data.receiveSize * 1.0) / (data.totalSize * 1.0)) * 100)
  }
})
httpRequest.on('dataEnd', () => {
  LogUtil.error('[' + request.requestId +'] NetworkHttpClient loadData end')
  // 下载完毕
  let combineArray = this.combineArrayBuffers(arrayBuffers);
  onComplete(combineArray)
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

headersReceive的回调中没有responseCode

await promise.then(data =>{
  if (data == 200) {
    LogUtil.error('[' + request.requestId +'] NetworkHttpClient loadData finish')
  } else {
    LogUtil.error('[' + request.requestId +'] NetworkHttpClient loadData '+ "下载失败 code = " + data)
    if (data == 404) {
      onError(JSON.stringify(data))
    } else {
      onError(`HttpDownloadClient has error, http code = ` + JSON.stringify(data))
    }
  }
})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

现在的responseCode在最后返回,太晚了,在某些场景下会影响图片上屏

HarmonyOS
2025-01-10 09:48:25
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

httpRequest流式请求headerReceive监听回调结果不包含状态码

如需采用流式数据下载文件可使用rcp上提供downloadToStream能力处理

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/remote-communication-rcp-V5#section25231139104519

分享
微博
QQ
微信
回复
2025-01-10 11:34:44
相关问题
HarmonyOS httpRequest,caPath路径写法
684浏览 • 1回复 待解决
怎么在httpRequest添加cookie
1369浏览 • 1回复 待解决
HarmonyOS 如何拿到header属性字段
573浏览 • 1回复 待解决
HarmonyOS 图片上传httpRequest格式问题
593浏览 • 1回复 待解决
HarmonyOS 网络请求header
712浏览 • 1回复 待解决
HarmonyOS config可以设置headerRange吗?
591浏览 • 1回复 待解决
HarmonyOS webview怎么添加header
614浏览 • 1回复 待解决
HamonyOS 设置webview请求头header
1422浏览 • 1回复 待解决