HarmonyOS 网络请求包含域名(多个)和证书(两套)动态切换功能的封装示例

HarmonyOS
2024-12-23 16:12:06
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

参考示例:

import { rcp } from '@kit.RemoteCommunicationKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct demo {
  @State host: string = 'http://huawei.com';
  @State httpMethod: string = "POST";
  @State caPath: string = '/path/dir/';

  fetch() {
    // Configure security settings
    const securityConfig: rcp.SecurityConfiguration = {
      remoteValidation: "system",
      certificate: {
        content: "-----BEGIN CERTIFICATE-----",
        type: "PEM",
        key: this.caPath,
        keyPassword: "your-password",
      },
      serverAuthentication: {
        credential: {
          username: "exampleUser",
          password: "examplePassword",
        },
        authenticationType: "basic",
      },
    };
    // Use the security configuration in the session creation
    const session = rcp.createSession({ requestConfiguration: { security: securityConfig } });

    console.info('host:' + this.host);
    console.info('httpMethod:' + this.httpMethod);
    session.fetch(new rcp.Request(this.host, this.httpMethod)).then((response) => {
      console.info(`Response succeed: ${response}`);
    }).catch((err: BusinessError) => {
      console.error(`err: err code is ${err.code}, err message is ${err.message}`);
    });
  }

  build() {
    Column() {
      Text('点击按钮切换域名和证书')
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
        .margin({ top: 20 })

      Button("GET")
        .margin({ top: 20 })
        .fontColor(Color.Blue)
        .onClick(() => {
          this.host = 'http://huawei.com'
          this.caPath = '/*'
          this.fetch()
        })

      Button("POST")
        .margin({ top: 20 })
        .fontColor(Color.Blue)
        .onClick(() => {
          this.host = 'http://huawei.com'
          this.caPath = '/*'
          this.fetch()
        })
    }
    .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.
分享
微博
QQ
微信
回复
2024-12-23 20:06:27
相关问题
两套字体库,应该怎么使用
761浏览 • 1回复 待解决
HarmonyOS 网络请求示例
948浏览 • 1回复 待解决
HarmonyOS http网络请求封装Demo
906浏览 • 1回复 待解决
证书锁定功能示例有哪些?
1456浏览 • 1回复 待解决
HarmonyOS 网络发起多个并发请求
903浏览 • 1回复 待解决
HarmonyOS rcp网络请求证书配置
938浏览 • 1回复 待解决
HarmonyOS 网络请求跳过ssl证书校验
1192浏览 • 1回复 待解决
网络请求怎么进行SSL证书认证?
4348浏览 • 1回复 待解决
websockethttp数据请求示例
1749浏览 • 1回复 待解决
HarmonyOS 网络接口请求查看功能
722浏览 • 1回复 待解决
ArkTS 网络请求 接口动态传参
1306浏览 • 1回复 待解决