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

HarmonyOS
3天前
浏览
收藏 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%')
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
两套字体库,应该怎么使用
414浏览 • 1回复 待解决
HarmonyOS 网络请求示例
53浏览 • 1回复 待解决
证书锁定功能示例有哪些?
776浏览 • 1回复 待解决
HarmonyOS 网络发起多个并发请求
63浏览 • 1回复 待解决
HarmonyOS rcp网络请求证书配置
50浏览 • 1回复 待解决
网络请求怎么进行SSL证书认证?
3482浏览 • 1回复 待解决
websockethttp数据请求示例
783浏览 • 1回复 待解决
ArkTS 网络请求 接口动态传参
516浏览 • 1回复 待解决
webview如何实现网络请求拦截功能
2261浏览 • 1回复 待解决
网络请求rcphttpRequest关系区别
480浏览 • 1回复 待解决