HarmonyOS http get请求不到内容

有一个网址,不管是编码前,还是编码后,都无法通过get请求拿到内容.但是链接地址都可以在浏览器打开,请问这个链接该如何请求.

HarmonyOS
2024-12-24 15:45:26
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

可以使用requestInStream来解决问题,具体在官网文档处了解,链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/http-request-V5#requestinstream%E6%8E%A5%E5%8F%A3%E5%BC%80%E5%8F%91%E6%AD%A5%E9%AA%A4

//参考demo如下
import { rcp } from "@kit.RemoteCommunicationKit";
import { BusinessError } from '@kit.BasicServicesKit';
import { promptAction } from '@kit.ArkUI';
import http from '@ohos.net.http';
import { util } from '@kit.ArkTS';


@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  get() {
    // 每一个httpRequest对应一个HTTP请求任务,不可复用
    let httpRequest = http.createHttp();
    // 用于订阅HTTP响应头事件
    httpRequest.on('headersReceive', (header: Object) => {
      console.info('header: ' + JSON.stringify(header));
    });
    // 用于订阅HTTP流式响应数据接收事件
    let res = new ArrayBuffer(0);
    httpRequest.on('dataReceive', (data: ArrayBuffer) => {
      const newRes = new ArrayBuffer(res.byteLength + data.byteLength);
      const resView = new Uint8Array(newRes);
      resView.set(new Uint8Array(res));
      resView.set(new Uint8Array(data), res.byteLength);
      res = newRes;
      console.info('res length: ' + res.byteLength);
      let textDecoder = util.TextDecoder.create('utf-8');
      let val = textDecoder.decodeWithStream(resView as Uint8Array);
      //在此处获取请求内容
      console.info('请求获取的内容为: ' + val);
    });
    // 用于订阅HTTP流式响应数据接收完毕事件
    httpRequest.on('dataEnd', () => {
      console.info('No more data in response, data receive end');
    });
    // 用于订阅HTTP流式响应数据接收进度事件
    class Data {
      receiveSize: number = 0;
      totalSize: number = 0;
    }
    httpRequest.on('dataReceiveProgress', (data: Data) => {
      console.log("dataReceiveProgress receiveSize:" + data.receiveSize + ", totalSize:" + data.totalSize);
    });

    let streamInfo: http.HttpRequestOptions = {
      method: http.RequestMethod.GET,  // 可选,默认为http.RequestMethod.GET
      // 开发者根据自身业务需要添加header字段
      // header: {
      //   'Content-Type': 'application/json'
      // },
      // 当使用POST请求时此字段用于传递请求体内容,具体格式与服务端协商确定
      // extraData: "data to send",
      expectDataType:  http.HttpDataType.STRING,// 可选,指定返回数据的类型
      usingCache: true, // 可选,默认为true
      priority: 1, // 可选,默认为1
      connectTimeout: 60000, // 可选,默认为60000ms
      readTimeout: 60000, // 可选,默认为60000ms。若传输的数据较大,需要较长的时间,建议增大该参数以保证数据传输正常终止
      usingProtocol: http.HttpProtocol.HTTP1_1 // 可选,协议类型默认值由系统自动指定
    }

    httpRequest.requestInStream(
      // 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
      "url地址",
      streamInfo, (err: BusinessError, data: number) => {
      console.error('error:' + JSON.stringify(err));
      console.info('ResponseCode :' + JSON.stringify(data));
      // 取消订阅HTTP响应头事件
      httpRequest.off('headersReceive');
      // 取消订阅HTTP流式响应数据接收事件
      httpRequest.off('dataReceive');
      // 取消订阅HTTP流式响应数据接收进度事件
      httpRequest.off('dataReceiveProgress');
      // 取消订阅HTTP流式响应数据接收完毕事件
      httpRequest.off('dataEnd');
      // 当该请求使用完毕时,调用destroy方法主动销毁
      httpRequest.destroy();
    }
    );
  }

  build() {
    Column() {
      Button('点击一下').onClick(()=>{
        this.get()
        promptAction.showToast({message:'方法已执行'})
      })
    }
    .justifyContent(FlexAlign.Center)
    .height('100%')
    .width('100%')
  }
}
  • 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.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
分享
微博
QQ
微信
回复
2024-12-24 16:38:36
相关问题
HTTP GET请求时如何传递参数?
4171浏览 • 1回复 待解决
http request 请求不到接口数据
6065浏览 • 1回复 待解决
HarmonyOS HTTP-post请求接收不到参数
1054浏览 • 1回复 待解决
网络请求-GET请求传参
1528浏览 • 1回复 待解决
HarmonyOS Map类型调用get获取不到
632浏览 • 1回复 待解决
HarmonyOS RCP GET请求、POST请求如何传参
1065浏览 • 1回复 待解决
HarmonyOS 取消http请求
1208浏览 • 1回复 待解决
HarmonyOS http请求封装
918浏览 • 1回复 待解决
Js 荣耀手机get请求失败
5984浏览 • 1回复 待解决
使用rcp模块能力发送Get请求
2066浏览 • 1回复 待解决
HarmonyOS http请求的封装
963浏览 • 1回复 待解决
HarmonyOS http请求返回2300007
1940浏览 • 1回复 待解决
HarmonyOS Http请求头问题咨询
1143浏览 • 1回复 待解决
HarmonyOS http post请求参数传递
1128浏览 • 1回复 待解决
HarmonyOS http 请求 post 参数问题
2419浏览 • 1回复 待解决