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%') 
  } 
}
分享
微博
QQ
微信
回复
2024-09-18 17:58:31
相关问题
最新5.0如何开启模拟器
741浏览 • 1回复 待解决
模拟器可以配置代理吗?
395浏览 • 1回复 待解决
HarmonyOS 模拟器无法安装应用
162浏览 • 1回复 待解决
真机和模拟器安装失败
11403浏览 • 2回复 待解决
真机安装失败,模拟器安装成功
9862浏览 • 4回复 已解决
模拟器无法启动该怎么解决
474浏览 • 1回复 待解决
启动本地模拟器,提示Haxm安装失败
679浏览 • 1回复 待解决
模拟器安装应用包报错
184浏览 • 1回复 待解决
模拟器启动失败,是哪里出了毛病
8661浏览 • 4回复 待解决
HarmonyOS如何下载模拟器
676浏览 • 1回复 待解决
HarmonyOS 模拟器黑屏
151浏览 • 1回复 待解决
HarmonyOS 模拟器设备IP如何配置
180浏览 • 1回复 待解决
DevEco模拟器无法启动
4577浏览 • 0回复 待解决
HarmonyOS 模拟器相册问题
157浏览 • 1回复 待解决
模拟器能否跳过联网?
186浏览 • 1回复 待解决