openharmony 鸿蒙 capi-net-http-interceptor-h

2026-08-25 浏览 (1)

http_interceptor.h

概述

定义HTTP全局拦截器模块的接口。通过拦截器,开发者可以监控HTTP流量,实现日志记录功能。

  • 只读拦截器限制:请勿在只读拦截器中修改请求和响应的内容或释放指针。即使进行修改,该修改也不会对请求生效,但可能会影响后续只读拦截器对数据包内容的读取。此外,在只读拦截器中设置 OH_ABORT 也不会生效。
  • 支持的组件:全局拦截器目前支持 @ohos.net.httpnet_http.hrcp@ohos.request.cacheDownload
  • 触发条件
    • 不拦截自动重定向的中间过程(仅暴露最终响应)。
    • 不拦截缓存命中时的响应(因无实际网络请求)。

引用文件: <network/netstack/http_interceptor.h>

库: libhttp_interceptor.so

系统能力: SystemCapability.Communication.NetStack

起始版本: 24

相关模块: netstack

汇总

函数

名称描述
int32_t OH_Http_AddReadOnlyInterceptor(struct OH_Http_Interceptor *interceptor)添加一个HTTP全局只读拦截器。
int32_t OH_Http_RemoveInterceptor(struct OH_Http_Interceptor *interceptor)删除指定的HTTP全局拦截器。
int32_t OH_Http_RemoveAllInterceptors(int32_t groupId)删除指定组ID的所有HTTP拦截器。
int32_t OH_Http_StartAllInterceptors(int32_t groupId)启用指定组ID的所有HTTP拦截器。
int32_t OH_Http_StopAllInterceptors(int32_t groupId)停用指定组ID的所有HTTP拦截器。

函数说明

OH_Http_AddReadOnlyInterceptor()

int32_t OH_Http_AddReadOnlyInterceptor(struct OH_Http_Interceptor *interceptor)

描述

添加一个HTTP全局只读拦截器。

系统能力: SystemCapability.Communication.NetStack

起始版本: 24

参数:

参数项描述
struct OH_Http_Interceptor *interceptor待添加的拦截器,指向OH_Http_Interceptor结构体的指针。

返回:

类型说明
int32_t返回值为0表示执行成功;返回值为201表示权限被拒绝;返回值为401表示参数错误(如指针为nullptr,或不支持所添加的拦截器类型)。详细错误码请参考OH_HTTP_RESULT_OKOH_HTTP_PERMISSION_DENIEDOH_HTTP_PARAMETER_ERROR

OH_Http_RemoveInterceptor()

int32_t OH_Http_RemoveInterceptor(struct OH_Http_Interceptor *interceptor)

描述

删除指定的HTTP全局拦截器。

系统能力: SystemCapability.Communication.NetStack

起始版本: 24

参数:

参数项描述
struct OH_Http_Interceptor *interceptor待删除的拦截器,指向OH_Http_Interceptor结构体的指针。

返回:

类型说明
int32_t返回值为0表示执行成功;返回值为201表示权限被拒绝;返回值为401表示参数错误(如指针为 nullptr)。详细错误码请参考OH_HTTP_RESULT_OKOH_HTTP_PERMISSION_DENIEDOH_HTTP_PARAMETER_ERROR

OH_Http_RemoveAllInterceptors()

int32_t OH_Http_RemoveAllInterceptors(int32_t groupId)

描述

删除指定组ID的所有HTTP拦截器。

  • 组ID由应用在创建拦截器时自行分配和管理。
  • 若应用内部多个模块使用拦截器,必须合理规划组ID,避免冲突。
  • 组ID冲突可能导致调用此函数时意外删除其他模块的拦截器。

系统能力: SystemCapability.Communication.NetStack

起始版本: 24

参数:

参数项描述
int32_t groupId拦截器组ID。

返回:

类型说明
int32_t返回值为0表示执行成功;返回值为201表示权限被拒绝。详细错误码请参考OH_HTTP_RESULT_OKOH_HTTP_PERMISSION_DENIED

OH_Http_StartAllInterceptors()

int32_t OH_Http_StartAllInterceptors(int32_t groupId)

描述

启用指定组ID的所有HTTP拦截器。

系统能力: SystemCapability.Communication.NetStack

起始版本: 24

参数:

参数项描述
int32_t groupId拦截器组ID。

返回:

类型说明
int32_t返回值为0表示执行成功;返回值为201表示权限被拒绝。详细错误码请参考OH_HTTP_RESULT_OKOH_HTTP_PERMISSION_DENIED

OH_Http_StopAllInterceptors()

int32_t OH_Http_StopAllInterceptors(int32_t groupId)

描述

停用指定组ID的所有HTTP拦截器。

系统能力: SystemCapability.Communication.NetStack

起始版本: 24

参数:

参数项描述
int32_t groupId拦截器组ID。

返回:

类型说明
int32_t返回值为0表示执行成功;返回值为201表示权限被拒绝。详细错误码请参考OH_HTTP_RESULT_OKOH_HTTP_PERMISSION_DENIED

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-netconnection-netconn-connectionproperties

openharmony 鸿蒙 errorcode-net-mdns

openharmony 鸿蒙 js-apis-net-ethernet-sys

openharmony 鸿蒙 errorcode-net-http

openharmony 鸿蒙 capi-netstack-http-proxy

openharmony 鸿蒙 js-apis-system-fetch

openharmony 鸿蒙 errorcode-kernel

openharmony 鸿蒙 capi-netstack-netstack-certblob

openharmony 鸿蒙 js-apis-net-mdns

openharmony 鸿蒙 capi-netstack-websocket-closeoption

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/7XNjKk0A