HarmonyOS鸿蒙Next中avplayer播放网络视频如何忽略https证书验证
HarmonyOS鸿蒙Next中avplayer播放网络视频如何忽略https证书验证 avplayer 播放网络视频 如何忽略https证书验证
【背景知识】
Remote Communication Kit提供了网络数据请求功能,相较于Network Kit中HTTP请求能力,更具易用性,且拥有更多的功能。Remote Communication Kit支持忽略证书校验。参考文档:自定义证书校验。
【解决方案】
方案一:
自定义requestConfig对象,重写ValidationCallback。remoteValidation在实际的证书验证中,这个函数会检查证书的有效性等信息,这里返回true来忽略证书校验,样例代码如下:
import { BusinessError } from '@ohos.base';
import promptAction from '@ohos.promptAction';
import { rcp } from '@kit.RemoteCommunicationKit';
import { util } from '@kit.ArkTS';
async function testRcpConnection() {
this.message = '正在建立连接...';
try {
const requestConfig: rcp.Configuration = {
security: {
remoteValidation: (context: rcp.ValidationContext) => {
// 重写ValidationCallback,直接返回true表示验证通过
console.info("[index]证书验证");
return true;
}
}
};
// 服务器地址
const kHttpServerAddress = "https://xxx.xx.xx.x:xxxx";
// 创建一个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.decodeWithStream(new Uint8Array(resp.body));
// 显示提示信息,包含响应成功的消息和响应体内容
promptAction.showToast({ message: '[index]Response succeeded: ' + body });
} catch (error) {
let err = error as BusinessError;
this.message = `[index]连接失败: ${err.code}`;
promptAction.showToast({ message: `错误: ${err.message}` });
console.error("[index]连接错误:", err);
}
}
方案二:
设置remoteValidation参数为skip,用于跳过证书验证,样例代码如下:
// Configure security settings
const securityConfig: rcp.SecurityConfiguration = {
remoteValidation: "skip"
};
// Use the security configuration in the session creation
const sessionWithSecurityConfig = rcp.createSession({ requestConfiguration: { security: securityConfig } });
更多关于HarmonyOS鸿蒙Next中avplayer播放网络视频如何忽略https证书验证的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
如果是在测试环境使用自签名证书,可以在应用配置文件中声明网络权限:ohos.permission.INTERNET;使用 HTTP 协议替代 HTTPS 协议进行临时测试(需确保测试服务器支持)
但正式发布时需恢复合法证书
期待HarmonyOS能在未来推出更多针对特定场景的优化功能。
在HarmonyOS Next中,AVPlayer默认遵循HTTPS证书验证规范。要忽略证书验证,需通过自定义实现。使用http
协议替代https
可绕过验证,但这不安全。若必须使用https
,需在应用层实现自定义网络请求,获取视频数据后通过AVPlayer
播放。目前鸿蒙未提供直接忽略HTTPS证书验证的API。注意:忽略证书验证会降低安全性。
在HarmonyOS Next中,AVPlayer默认会验证HTTPS证书。如果需要忽略证书验证,可以通过以下方式实现:
- 使用自定义的SSLContext:
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
- 在播放前设置:
AVPlayer avPlayer = new AVPlayer(context);
avPlayer.setSource(new AVSource.Builder()
.setUri("https://your-video-url")
.build());
注意:忽略证书验证会降低安全性,仅建议在开发和测试环境中使用。生产环境应使用有效的HTTPS证书。