HarmonyOS http能力抓包配置?

如题:HarmonyOS http能力抓包配置?

阅读 468
1 个回答

相关配置代码如下:

requestHttp(url: string): Promise<string> {
  return new Promise((resolve, reject) => {
    getContext(this).area = 0
    let context: Context = getContext(this);
    const keyPemComponent = context.resourceManager.getRawFileContentSync('testCa.pem')
    let filesDir: string = context.filesDir
    let filePath = filesDir + "/testCer.pem";
    let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
    fs.writeSync(file.fd, keyPemComponent.buffer);
    fs.fsyncSync(file.fd);
    fs.closeSync(file);
    let httpRequest = http.createHttp();
    httpRequest.request(url, {method: http.RequestMethod.GET, header:{ "Content-Type":"application/json" }, caPath : filePath }, (err: BusinessError, data) => {
      console.info('url:' + JSON.stringify(url));
      if (!err) { //响应耗时时间
        console.info('responseCode:' + JSON.stringify(data.responseCode));
        console.info('result:' + JSON.stringify(data.result));
      } else {
        console.info('error:' + JSON.stringify(err));
      }
    })
  });
}

testCa.pem是放在资源目录下的证书,在rawfile目录下。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进