Remote Communication Kit提供了网络数据请求功能。相较于传统HTTP请求能力,它拥有更优的性能表现与更丰富的配置项。
针对证书校验,Remote Communication Kit提供了两种灵活的规避/自定义校验机制:
自定义requestConfig对象,重写ValidationCallback。remoteValidation在实际的证书验证中,这个函数会检查证书的有效性等信息,这里返回true来忽略证书校验,样例代码如下:
const requestConfig: rcp.Configuration = {
security: {
remoteValidation: () => {
// 重写ValidationCallback,直接返回true表示验证通过
console.info('[index]证书验证');
return true;
}
} 设置remoteValidation参数为skip,用于跳过证书验证,样例代码如下:
const securityConfig: rcp.SecurityConfiguration = {
remoteValidation: 'skip'
}; 完整示例代码集成了两种方案处理证书校验,代码如下:
import { BusinessError } from '@ohos.base';
import promptAction from '@ohos.promptAction';
import { rcp } from '@kit.RemoteCommunicationKit';
import { util } from '@kit.ArkTS';
@Entry
@Component
struct RcpConnection {
build() {
RelativeContainer() {
Button('自定义证书验证')
.id('btn1')
.fontSize(14)
.fontWeight(FontWeight.Bold)
.width('60%')
.height('50vp')
.padding({
left: '16vp',
right: '16vp',
top: '12vp',
bottom: '12vp'
})
.alignRules({
middle: { anchor: '__container__', align: HorizontalAlign.Center },
bottom: { anchor: 'btn2', align: VerticalAlign.Top }
})
.margin({ bottom: '20vp' })
.onClick(() => {
this.testRcpConnection();
});
Button('跳过证书验证')
.id('btn2')
.fontSize(14)
.fontWeight(FontWeight.Bold)
.width('60%')
.height('50vp')
.padding({
left: '16vp',
right: '16vp',
top: '12vp',
bottom: '12vp'
})
.alignRules({
middle: { anchor: '__container__', align: HorizontalAlign.Center },
center: { anchor: '__container__', align: VerticalAlign.Center }
})
.onClick(() => {
this.testRcpConnection2();
});
}
.height('100%')
.width('100%');
}
async testRcpConnection() {
try {
const requestConfig: rcp.Configuration = {
security: {
remoteValidation: () => {
// 重写ValidationCallback,直接返回true表示验证通过
console.info('[index]证书验证');
return true;
}
}
};
// 服务器地址
const kHttpServerAddress = 'xx.xx.xx';
// 创建一个get请求对象
const request = new rcp.Request(kHttpServerAddress, 'GET');
// 传入自定义的配置项,处理请求
const session = rcp.createSession({ requestConfiguration: requestConfig });
const resp = await session.fetch(request);
let decoder: util.TextDecoder = util.TextDecoder.create('utf-8');
let body: string = decoder.decodeToString(new Uint8Array(resp.body));
// 显示提示信息,包含响应成功的消息和响应体内容
promptAction.openToast({ message: '连接成功' });
console.info('连接成功: ' + body);
} catch (error) {
let err = error as BusinessError;
promptAction.openToast({ message: `错误: ${err.message}` });
console.error('[index]连接错误:', err);
}
}
async testRcpConnection2() {
try {
const securityConfig: rcp.SecurityConfiguration = {
remoteValidation: 'skip'
};
// 服务器地址
const kHttpServerAddress = 'xx.xx.xx';
// 创建一个get请求对象
const request = new rcp.Request(kHttpServerAddress, 'GET');
// 传入自定义的配置项,处理请求
const sessionWithSecurityConfig = rcp.createSession({ requestConfiguration: { security: securityConfig } });
const resp = await sessionWithSecurityConfig.fetch(request);
let decoder: util.TextDecoder = util.TextDecoder.create('utf-8');
let body: string = decoder.decodeToString(new Uint8Array(resp.body));
// 显示提示信息,包含响应成功的消息和响应体内容
promptAction.openToast({ message: '连接成功' });
console.info('连接成功: ' + body);
} catch (error) {
let err = error as BusinessError;
promptAction.openToast({ message: `错误: ${err.message}` });
console.error('[index]连接错误:', err);
}
}
}