使用http请求网络地址

通过HTTP发起一个数据请求

HarmonyOS
2024-05-28 20:53:41
1731浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
人间冰美式

使用的核心API

http请求API

核心代码解释

http请求接口通过POST和PUT请求获取网页中表格数据,请求部分demo如下:

requestHttp(url: string, method: http.RequestMethod): Promise<string> { 
    return new Promise((resolve, reject) => { 
  
      let httpRequest = http.createHttp(); 
  
      // 用于订阅HTTP响应头,此接口会比request请求先返回 
      httpRequest.on('headersReceive', (header) => { 
        console.info(`url=${url} is error ${JSON.stringify(header)}}`); 
        // LogUtil.info(TAG, 'header: ' + JSON.stringify(header)); 
      }); 
  
      //http请求接口 
      httpRequest.request( 
        url, 
        { 
          // 可选,指定返回数据的类型 
          expectDataType: http.HttpDataType.STRING, 
          // 可选,默认为http.RequestMethod.GET 
          method: method, 
          // 开发者根据自身业务需要添加header字段 
          header: { 
            'content-type': 'multipart/form-data', 
          }, 
          extraData:{ 'test1' : '122334455' }, 
          usingCache: false, // 可选,默认为true 
          priority: 1, // 可选,默认为1 
          connectTimeout: 10000, // 可选,默认为60000ms 
          readTimeout: 10000, // 可选,默认为60000ms 
        }, (err, data) => { 
        console.info(`------------ requestUrl: ${JSON.stringify(url)}----------`); 
        if (!err) { 
          // data.result为HTTP响应内容,可根据业务需要进行解析 
          this.result = `---------- connect Result : ---------- 
responseCode:${JSON.stringify(data.responseCode)} 
header:${JSON.stringify(data.header)} 
cookies:${JSON.stringify(data.cookies)} 
          -------------------------------------------`; 
          console.info(`Result:${JSON.stringify(data.result)}`); 
          console.info(`responseCode:${JSON.stringify(data.responseCode)}`); 
          console.info(`header:${JSON.stringify(data.header)}`); 
          console.info(`cookies:${JSON.stringify(data.cookies)}`); 
  
          resolve(JSON.stringify(data.result)); 
        } else { 
          this.result = `url=${url} is error ${JSON.stringify(err)}}`; 
          console.info(`url=${url} is error ${JSON.stringify(err)}}`); 
          httpRequest.off('headersReceive'); 
  
          reject(JSON.stringify(err)); 
        } 
      }) 
    }); 
} 
 
  • 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.

注明适配的版本信息

  • IDE:DevEco Studio 3.1.0.501
  • SDK:HarmoneyOS 3.2.14.6
分享
微博
QQ
微信
回复
2024-05-29 21:58:55
相关问题
HarmonyOS 网络地址格式校验--
712浏览 • 1回复 待解决
网络地址建立socket连接
1572浏览 • 1回复 待解决
HarmonyOS Image图片部分网络地址不显示
804浏览 • 1回复 待解决
HarmonyOS http网络请求封装的Demo
923浏览 • 1回复 待解决
HTTP请求使用同一SESSIONID
1364浏览 • 1回复 待解决
网络请求:gzip压缩使用
1061浏览 • 1回复 待解决
如何使用arkts进行http请求通信
303浏览 • 0回复 待解决
使用http模块实现数据流请求
2023浏览 • 1回复 待解决
网络请求使用gzip压缩数据
3029浏览 • 1回复 待解决