5.0的模拟器要如何安装代理呢?

将模拟器设置了代理IP和端口,但是证书不知道要如何导入进去。

HarmonyOS
2024-09-18 11:37:26
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

1、关于模拟器网安装网络代理配置,请参考链接地址:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-emulator-more-features-0000001886036897-V5#section206461549731

2、证书导入资料:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/ide-emulator-faqs-0000001840200954-V5#section28179477315

实现基本思路:通过app将证书文件带入模拟器,在app中通过picker创建文件、读取证书、写入设备的文件管理,之后通过证书管理安装证书。实现代码如下,创建1个新代码工程,复制以下代码到entry的Index.ets,证书xxx.perm保存到在extry\src\main\resource\rowfile,编译后在最新5.x的模拟器上执行(代码格式回帖比较乱,

copy后在IDE内可格式化):

import fs from '@ohos.file.fs'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
import { buffer } from '@kit.ArkTS'; 
import { picker } from '@kit.CoreFileKit'; 
import { resourceManager } from '@kit.LocalizationKit'; 
import { common, Want } from '@kit.AbilityKit'; 
 
@Entry 
@Component 
struct Index { 
  certFileName: string = "xxx.pem"; 
  saveUri: string = ""; 
  certFileData: string = ""; 
  @State status: string = ""; 
 
  createPem() : void { 
    try { 
      let docOpts = new picker.DocumentSaveOptions 
      docOpts.newFileNames = [this.certFileName] 
      let docPicker = new picker.DocumentViewPicker 
      docPicker.save(docOpts).then((result: Array<string>) => { 
        console.error('DocumentViewPicker.save succeed. ' + JSON.stringify(result)); 
        this.saveUri = result[0] 
        this.status = "Create file succeed.\nUri: " + this.saveUri 
      }).catch((err: BusinessError) => { 
        console.error('DocumentViewPicker.save failed with err: ' + JSON.stringify(err)); 
        this.status = "Create file failed.\nError message:" + err.message 
      }) 
    } catch (e) { 
      let err = e as BusinessError 
      console.error('DocumentViewPicker failed with err: ' + JSON.stringify(err)); 
      this.status = "Create file failed.\nError message:" + err.message 
    } 
  } 
 
  getCertData(filename: string) : void { 
    getContext().resourceManager.getRawFd(filename).then((value: resourceManager.RawFileDescriptor) => { 
      console.error('getRawFd succeed id is ' + value.fd); 
      console.error('getRawFd succeed offset is ' + value.offset); 
      console.error('getRawFd succeed length is ' + value.length); 
 
      let arrayBuffer = new ArrayBuffer(4096); 
      fs.read(value.fd, arrayBuffer, {offset: value.offset, length: value.length}).then((readLen: number) => { 
        console.info("read file data succeed"); 
        let buf = buffer.from(arrayBuffer, 0, readLen); 
        console.info(`The content of file: ${buf.toString()}`); 
        this.certFileData = buf.toString(); 
        this.status = "Get cert data succeed.\n" 
      }).catch((err: BusinessError) => { 
        console.error("read file data failed with error message: " + err.message + ", error code: " + err.code); 
        this.status = "Get cert data failed.\nError message:" + err.message 
      }) 
    }).catch((err: BusinessError) => { 
      console.error('getRawFd failed with err: ' + JSON.stringify(err)); 
      this.status = "Get cert data failed.\nError message:" + err.message 
    }) 
  } 
 
  writeFile(uri: string) : void { 
    try { 
      let file = fs.openSync(uri, fs.OpenMode.READ_WRITE); 
      let certData = this.certFileData; 
      // 写入一段内容至文件 
      fs.writeSync(file.fd, certData); 
      // 从文件读取一段内容 
      let arrayBuffer = new ArrayBuffer(4096); 
      class Option { 
        public offset: number = 0; 
        public length: number = 0; 
      } 
 
      let option = new Option(); 
      option.length = arrayBuffer.byteLength; 
      let readLen = fs.readSync(file.fd, arrayBuffer, option); 
      let buf = buffer.from(arrayBuffer, 0, readLen); 
      console.info("the content of file: " + buf.toString()); 
      console.info("test.txt dir:" + file.path); 
      fs.closeSync(file); 
      this.status = "Write data succeed." 
    } catch (e) { 
      let err = e as BusinessError 
      console.error('fs.openSync failed with err: ' + JSON.stringify(err)); 
      this.status = "Write data failed.\nError message:" + err.message 
    } 
  } 
 
  openCertManager() : void { 
    let wantInfo : Want = { 
      deviceId: '', // deviceId为空表示本设备 
      bundleName: 'com.ohos.certmanager', 
      abilityName: 'MainAbility', 
    } 
    // context为调用方UIAbility的AbilityContext 
    let context = getContext(this) as common.UIAbilityContext 
    context.startAbility(wantInfo).then(() => { 
      console.error('openCertManager succeed'); 
      this.status = "Open CertManager succeed." 
    }).catch((err: BusinessError) => { 
      console.error('openCertManager failed with err: ' + JSON.stringify(err)); 
      this.status = "Open CertManager failed.\nError message:" + err.message 
    }) 
  } 
 
  build() { 
    Row() { 
      Column() { 
        Text("Just click one by one.") 
          .fontSize(30) 
          .fontWeight(FontWeight.Bold) 
        Blank().height('20vp') 
        Text("Step1: Create File(.pem)") 
          .fontSize(25) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            this.createPem() 
          }) 
        Blank().height('10vp') 
        Text("Step2: Get certfile datas.") 
          .fontSize(25) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            this.getCertData(this.certFileName) 
          }) 
        Blank().height('10vp') 
        Text("Step3: Write data to file.") 
          .fontSize(25) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            this.writeFile(this.saveUri) 
          }) 
        Blank().height('10vp') 
        Text("Step4: Open CertManager") 
          .fontSize(25) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            this.openCertManager() 
          }) 
        Blank().height('10vp') 
        Text(this.status) 
 
      } 
      .width('100%') 
    } 
    .height('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.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
分享
微博
QQ
微信
回复
2024-09-18 17:58:31
相关问题
最新5.0如何开启模拟器
1568浏览 • 1回复 待解决
模拟器可以配置代理吗?
1127浏览 • 1回复 待解决
HarmonyOS 模拟器无法安装应用
839浏览 • 1回复 待解决
真机和模拟器安装失败
12293浏览 • 2回复 待解决
真机安装失败,模拟器安装成功
10719浏览 • 4回复 已解决
模拟器无法启动该怎么解决
1657浏览 • 1回复 待解决
启动本地模拟器,提示Haxm安装失败
1471浏览 • 1回复 待解决
模拟器安装应用包报错
989浏览 • 1回复 待解决
模拟器启动失败,是哪里出了毛病
9854浏览 • 4回复 待解决
HarmonyOS如何下载模拟器
2585浏览 • 1回复 待解决
HarmonyOS 模拟器黑屏
1009浏览 • 1回复 待解决
HarmonyOS 模拟器设备IP如何配置
1257浏览 • 1回复 待解决
关于模拟器存储消耗
304浏览 • 0回复 待解决
模拟器能否跳过联网?
822浏览 • 1回复 待解决