HarmonyOS 鸿蒙Next 忽略证书验证抓包https请求

发布于 1周前 作者 h691938207 来自 鸿蒙OS

HarmonyOS 鸿蒙Next 忽略证书验证抓包https请求
<markdown _ngcontent-txa-c237="" class="markdownPreContainer">

1. 背景

目前在鸿蒙中的网络请求大致有三种

[@ohos](/user/ohos).net.http:对应的三方库有axios

[@ohos](/user/ohos).net.socket:对应的三方库有【httpclient

@kit.RemoteCommunicationKit

1.1 抓包前的前置条件,需要安装Charles证书到真机上

具体步骤可参考这篇文章 https://juejin.cn/post/7378839984742613018

2. 问题

目前在API12 中我个人尝试使用[@ohos](/user/ohos).net.http 和[@ohos](/user/ohos).net.socket 均无法使用Charles抓包https请求。个人推测是没有对应的API来忽略Charles的用户证书【有哪位大佬解决了可以评论区评论下】

通过和一些业内大佬交流,小道消息得知,鸿蒙未来主流的网络框架是@kit.RemoteCommunicationKit [@ohos](/user/ohos).net.http[@ohos](/user/ohos).net.socket仅做功能维护

于是开始尝试阅读@kit.RemoteCommunicationKit 的开发文档,发现有一个remoteValidation 的属性,该属性的类型有【'system' | 'skip' | CertificateAuthority | ValidationCallback】。看到skip。感觉有戏

3. 解决方案

核心代码就是,对应的文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/remote-communication-rcp-V5#section1597118916425

request.configuration = {
  security: {
    remoteValidation: 'skip',
  },
};
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>
import { rcp } from "@kit.RemoteCommunicationKit";
import { BusinessError } from '@kit.BasicServicesKit';

const session = rcp.createSession(); const request = new rcp.Request(https://wanandroid.com/harmony/index/json,‘GET’); request.configuration = { security: { remoteValidation: ‘skip’, }, }; session.fetch(request).then((rep: rcp.Response) => { console.info(</span></span><span class="javascript"><span class="hljs-function"><span class="hljs-params"><span class="javascript">Response succeeded: ${rep}</span></span></span></span><span class="hljs-function"><span class="hljs-params">); }).catch((err: BusinessError) => { console.error(</span></span><span class="javascript"><span class="hljs-function"><span class="hljs-params"><span class="javascript">Response err: Code is ${err.code}, message is ${err.message}</span></span></span></span><span class="hljs-function"><span class="hljs-params">); }); <button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>

</markdown>

关于HarmonyOS 鸿蒙Next 忽略证书验证抓包https请求的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。
6 回复
你好,请问下第3步解决方案里代码用在哪里,这里没看明白

找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:https://www.bilibili.com/video/BV1S4411E7LY/?p=17

我只想抓个包啊,鸿蒙有没有类似于stream (iOS)的应用能够在手机上抓下包或者Charles也行啊,你这代码的意思是要自己写个app吗

HarmonyOS的开发者模式提供了很多实用的工具,方便我们进行调试和优化。

鸿蒙系统层面禁止抓三方APP

回到顶部