已合并
[issue]support global interceptor #140515
panjie创建于 4月20日
[issue]support global interceptor #140515
已合并
共 12 个文件变更+396-3
| @@ -18,6 +18,7 @@ | |||
| 18 | - [使用Socket访问网络](socket-connection.md) | 18 | - [使用Socket访问网络](socket-connection.md) |
| 19 | - [使用MDNS访问局域网服务](net-mdns.md) | 19 | - [使用MDNS访问局域网服务](net-mdns.md) |
| 20 | - [使用DNS解析域名](net-dns.md) | 20 | - [使用DNS解析域名](net-dns.md) |
| 21 | + - [使用HTTP全局拦截器(C/C++)](native-httpinterceptor-guidelines.md) | ||
| 21 | 22 | ||
| 22 | - 连接网络<!--network-kit-network-connecttion--> | 23 | - 连接网络<!--network-kit-network-connecttion--> |
| 23 | - [管理网络连接](net-connection-manager.md) | 24 | - [管理网络连接](net-connection-manager.md) |
| @@ -0,0 +1,388 @@ | |||||||
| 1 | +# 使用HTTP全局拦截器 (C/C++) | ||||||
| 2 | +<!--Kit: Network Kit--> | ||||||
| 3 | +<!--Subsystem: Communication--> | ||||||
| 4 | +<!--Owner: @wmyao_mm--> | ||||||
| 5 | +<!--Designer: @guo-min_net--> | ||||||
| 6 | +<!--Tester: @tongxilin--> | ||||||
| 7 | +<!--Adviser: @zhang_yixin13--> | ||||||
| 8 | + | ||||||
| 9 | +## 场景介绍 | ||||||
| 10 | + | ||||||
| 11 | +从API version 24开始,通过HTTP全局拦截器,开发者可以监控HTTP流量,实现日志记录功能。 | ||||||
| 12 | + | ||||||
| 13 | +## 接口说明 | ||||||
| 14 | + | ||||||
| 15 | +HTTP全局拦截器常用接口如下表所示,详细的接口说明请参考[http_interceptor.h](../reference/apis-network-kit/capi-net-http-interceptor-h.md)。 | ||||||
| 16 | + | ||||||
| 17 | + | ||||||
| 18 | +| 接口名 | 描述 | | ||||||
| 19 | +| -------- | -------- | | ||||||
| 20 | +| OH_Http_AddReadOnlyInterceptor(struct OH_Http_Interceptor *interceptor) | 添加一个HTTP全局只读拦截器。 | | ||||||
| 21 | +| OH_Http_RemoveInterceptor(struct OH_Http_Interceptor *interceptor) | 删除指定的HTTP全局拦截器。 | | ||||||
| 22 | +| OH_Http_RemoveAllInterceptors(int32_t groupId) | 删除指定组ID的所有HTTP全局拦截器。 | | ||||||
| 23 | +| OH_Http_StartAllInterceptors(int32_t groupId) | 启用指定组ID的所有HTTP全局拦截器。 | | ||||||
| 24 | +| OH_Http_StopAllInterceptors(int32_t groupId) | 停用指定组ID的所有HTTP全局拦截器。 | | ||||||
| 25 | + | ||||||
| 26 | +## 开发步骤 | ||||||
| 27 | + | ||||||
| 28 | +使用本文档涉及接口创建并使用HTTP全局拦截器时,需先创建Native C++工程,在源文件中封装相关接口,然后在ArkTS层调用封装好的接口,使用hilog或console.info等方法将日志打印到控制台或生成设备日志。 | ||||||
| 29 | + | ||||||
| 30 | +本文以添加HTTP全局只读响应拦截器、启用/停用拦截器、删除拦截器为例,提供具体的开发指导。 | ||||||
A ❌疑似检测出中文用词问题 错误原因:在技术文档中,"停用"通常用于设备、服务等具体对象,而"禁用"更常用于功能、模块。建议统一使用"禁用"。 建议修改为: 改动建议
![]() ![]() | |||||||
| 31 | + | ||||||
| 32 | +### 添加开发依赖 | ||||||
| 33 | + | ||||||
| 34 | +**添加动态链接库** | ||||||
| 35 | + | ||||||
| 36 | +CMakeLists.txt中添加以下lib: | ||||||
| 37 | + | ||||||
| 38 | +```txt | ||||||
| 39 | +libace_napi.z.so | ||||||
| 40 | +libhttp_interceptor.so | ||||||
| 41 | +``` | ||||||
| 42 | +**头文件** | ||||||
| 43 | + | ||||||
| 44 | +```c | ||||||
| 45 | +#include "napi/native_api.h" | ||||||
| 46 | +#include "network/netstack/http_interceptor.h" | ||||||
| 47 | +#include "network/netstack/http_interceptor_type.h" | ||||||
| 48 | +``` | ||||||
| 49 | +### 构建工程 | ||||||
| 50 | + | ||||||
| 51 | +1. 在源文件中编写调用该API的代码,实现HTTP全局拦截器的处理函数和相关操作。 | ||||||
| 52 | + | ||||||
| 53 | + <!-- @[HttpInterceptor_build_project](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/NetWork_Kit/NetWorkKit_Datatransmission/HTTP_interceptor_C/entry/src/main/cpp/napi_init.cpp) --> | ||||||
| 54 | + | ||||||
| 55 | + ``` C++ | ||||||
| 56 | + #include "napi/native_api.h" | ||||||
| 57 | + #include "network/netstack/http_interceptor.h" | ||||||
| 58 | + #include "network/netstack/http_interceptor_type.h" | ||||||
| 59 | + #include "hilog/log.h" | ||||||
| 60 | + | ||||||
| 61 | + #include <cstring> | ||||||
| 62 | + | ||||||
| 63 | + #undef LOG_DOMAIN | ||||||
| 64 | + #undef LOG_TAG | ||||||
| 65 | + #define LOG_DOMAIN 0x3200 // 全局domain宏,标识业务领域 | ||||||
| 66 | + #define LOG_TAG "HttpInterceptorDemo" // 全局tag宏,标识模块日志tag | ||||||
| 67 | + | ||||||
| 68 | + // 全局拦截器实例 | ||||||
| 69 | + static OH_Http_Interceptor g_responseInterceptor = { | ||||||
| 70 | + .groupId = 1, | ||||||
| 71 | + .stage = OH_STAGE_RESPONSE, | ||||||
| 72 | + .type = OH_TYPE_READ_ONLY, | ||||||
| 73 | + .enabled = 1, | ||||||
| 74 | + .handler = nullptr, | ||||||
| 75 | + }; | ||||||
| 76 | + | ||||||
| 77 | + // 日志打印辅助函数 | ||||||
| 78 | + void LogHeader(OH_Http_Interceptor_Headers *headers) | ||||||
| 79 | + { | ||||||
| 80 | + OH_LOG_INFO(LOG_APP, "---------------------header begin---------------------"); | ||||||
| 81 | + while (headers != nullptr) { | ||||||
| 82 | + if (headers->data != nullptr) { | ||||||
| 83 | + OH_LOG_INFO(LOG_APP, "%{public}s", headers->data); | ||||||
| 84 | + } | ||||||
| 85 | + headers = headers->next; | ||||||
| 86 | + } | ||||||
| 87 | + OH_LOG_INFO(LOG_APP, "---------------------header end---------------------"); | ||||||
| 88 | + } | ||||||
| 89 | + | ||||||
| 90 | + // 打印响应信息 | ||||||
| 91 | + void PrintResponseInfo(OH_Http_Interceptor_Response *response) | ||||||
| 92 | + { | ||||||
| 93 | + OH_LOG_INFO(LOG_APP, "-----PrintResponseInfo Begin-----"); | ||||||
| 94 | + if (response != nullptr) { | ||||||
| 95 | + OH_LOG_INFO(LOG_APP, "responseCode = %{public}d", response->responseCode); | ||||||
| 96 | + if (response->body.buffer != nullptr) { | ||||||
| 97 | + OH_LOG_INFO(LOG_APP, "body = %{public}s", response->body.buffer); | ||||||
| 98 | + } | ||||||
| 99 | + if (response->headers != nullptr) { | ||||||
| 100 | + LogHeader(response->headers); | ||||||
| 101 | + } | ||||||
| 102 | + | ||||||
| 103 | + OH_LOG_INFO(LOG_APP, "dns: %{public}lf", response->performanceTiming.dnsTiming); | ||||||
| 104 | + OH_LOG_INFO(LOG_APP, "tcp: %{public}lf", response->performanceTiming.tcpTiming); | ||||||
| 105 | + OH_LOG_INFO(LOG_APP, "tls: %{public}lf", response->performanceTiming.tlsTiming); | ||||||
| 106 | + OH_LOG_INFO(LOG_APP, "snd: %{public}lf", response->performanceTiming.firstSendTiming); | ||||||
| 107 | + OH_LOG_INFO(LOG_APP, "rcv: %{public}lf", response->performanceTiming.firstReceiveTiming); | ||||||
| 108 | + OH_LOG_INFO(LOG_APP, "tot: %{public}lf", response->performanceTiming.totalFinishTiming); | ||||||
| 109 | + OH_LOG_INFO(LOG_APP, "rdr: %{public}lf", response->performanceTiming.redirectTiming); | ||||||
| 110 | + OH_LOG_INFO(LOG_APP, "-----PrintResponseInfo End-----"); | ||||||
| 111 | + } | ||||||
| 112 | + } | ||||||
| 113 | + | ||||||
| 114 | + // 响应拦截器处理函数 | ||||||
| 115 | + OH_Interceptor_Result ResponseInterceptorHandler( | ||||||
| 116 | + OH_Http_Interceptor_Request *request, | ||||||
| 117 | + OH_Http_Interceptor_Response *response, | ||||||
| 118 | + int32_t *isModified) | ||||||
| 119 | + { | ||||||
| 120 | + (void)request; | ||||||
| 121 | + (void)isModified; | ||||||
| 122 | + | ||||||
| 123 | + if (response != nullptr) { | ||||||
| 124 | + OH_LOG_INFO(LOG_APP, "---Response Interceptor Handler---"); | ||||||
| 125 | + PrintResponseInfo(response); | ||||||
| 126 | + } | ||||||
| 127 | + return OH_CONTINUE; | ||||||
| 128 | + } | ||||||
| 129 | + | ||||||
| 130 | + // 添加只读响应拦截器 | ||||||
| 131 | + static napi_value AddResponseInterceptor(napi_env env, napi_callback_info info) | ||||||
| 132 | + { | ||||||
| 133 | + napi_value result; | ||||||
| 134 | + | ||||||
| 135 | + // 设置拦截器处理函数 | ||||||
| 136 | + g_responseInterceptor.handler = ResponseInterceptorHandler; | ||||||
| 137 | + | ||||||
| 138 | + // 添加拦截器 | ||||||
| 139 | + int ret = OH_Http_AddReadOnlyInterceptor(&g_responseInterceptor); | ||||||
| 140 | + | ||||||
| 141 | + OH_LOG_INFO(LOG_APP, "AddResponseInterceptor ret: %{public}d", ret); | ||||||
| 142 | + napi_create_int32(env, ret, &result); | ||||||
| 143 | + return result; | ||||||
| 144 | + } | ||||||
| 145 | + | ||||||
| 146 | + // 移除拦截器 | ||||||
| 147 | + static napi_value RemoveInterceptor(napi_env env, napi_callback_info info) | ||||||
| 148 | + { | ||||||
| 149 | + napi_value result; | ||||||
| 150 | + | ||||||
| 151 | + // 移除拦截器 | ||||||
| 152 | + int ret = OH_Http_RemoveInterceptor(&g_responseInterceptor); | ||||||
| 153 | + | ||||||
| 154 | + OH_LOG_INFO(LOG_APP, "RemoveInterceptor ret: %{public}d", ret); | ||||||
| 155 | + napi_create_int32(env, ret, &result); | ||||||
| 156 | + return result; | ||||||
| 157 | + } | ||||||
| 158 | + | ||||||
| 159 | + // 启用指定组的所有拦截器 | ||||||
| 160 | + static napi_value StartInterceptors(napi_env env, napi_callback_info info) | ||||||
| 161 | + { | ||||||
| 162 | + napi_value result; | ||||||
| 163 | + | ||||||
| 164 | + // 启用组ID为1的所有拦截器 | ||||||
| 165 | + int ret = OH_Http_StartAllInterceptors(1); | ||||||
| 166 | + | ||||||
| 167 | + OH_LOG_INFO(LOG_APP, "StartInterceptors ret: %{public}d", ret); | ||||||
| 168 | + napi_create_int32(env, ret, &result); | ||||||
| 169 | + return result; | ||||||
| 170 | + } | ||||||
| 171 | + | ||||||
| 172 | + // 停用指定组的所有拦截器 | ||||||
| 173 | + static napi_value StopInterceptors(napi_env env, napi_callback_info info) | ||||||
| 174 | + { | ||||||
| 175 | + napi_value result; | ||||||
| 176 | + | ||||||
| 177 | + // 停用组ID为1的所有拦截器 | ||||||
| 178 | + int ret = OH_Http_StopAllInterceptors(1); | ||||||
| 179 | + | ||||||
| 180 | + OH_LOG_INFO(LOG_APP, "StopInterceptors ret: %{public}d", ret); | ||||||
| 181 | + napi_create_int32(env, ret, &result); | ||||||
| 182 | + return result; | ||||||
| 183 | + } | ||||||
| 184 | + | ||||||
| 185 | + // 删除指定组的所有拦截器 | ||||||
| 186 | + static napi_value RemoveAllInterceptors(napi_env env, napi_callback_info info) | ||||||
| 187 | + { | ||||||
| 188 | + napi_value result; | ||||||
| 189 | + | ||||||
| 190 | + // 删除组ID为1的所有拦截器 | ||||||
| 191 | + int ret = OH_Http_RemoveAllInterceptors(1); | ||||||
| 192 | + | ||||||
| 193 | + OH_LOG_INFO(LOG_APP, "RemoveAllInterceptors ret: %{public}d", ret); | ||||||
| 194 | + napi_create_int32(env, ret, &result); | ||||||
| 195 | + return result; | ||||||
| 196 | + } | ||||||
| 197 | + ``` | ||||||
O 当前建议代码无改动
![]() ![]() | |||||||
| 198 | + | ||||||
| 199 | + 上述代码实现了一个HTTP全局只读响应拦截器,用于监控HTTP响应。在响应拦截器处理函数中,会打印响应的状态码、响应体、响应头以及性能指标等信息。 | ||||||
| 200 | + | ||||||
| 201 | +2. 初始化并导出通过N-API封装的`napi_value`类型对象,通过外部函数接口将函数提供给JavaScript调用。 | ||||||
| 202 | + | ||||||
| 203 | + <!-- @[HttpInterceptor_extern_c](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/NetWork_Kit/NetWorkKit_Datatransmission/HTTP_interceptor_C/entry/src/main/cpp/napi_init.cpp) --> | ||||||
| 204 | + | ||||||
| 205 | + ``` C++ | ||||||
| 206 | + EXTERN_C_START | ||||||
| 207 | + static napi_value Init(napi_env env, napi_value exports) | ||||||
| 208 | + { | ||||||
| 209 | + napi_property_descriptor desc[] = { | ||||||
| 210 | + {"AddResponseInterceptor", nullptr, AddResponseInterceptor, nullptr, nullptr, nullptr, napi_default, nullptr}, | ||||||
| 211 | + {"RemoveInterceptor", nullptr, RemoveInterceptor, nullptr, nullptr, nullptr, napi_default, nullptr}, | ||||||
| 212 | + {"StartInterceptors", nullptr, StartInterceptors, nullptr, nullptr, nullptr, napi_default, nullptr}, | ||||||
| 213 | + {"StopInterceptors", nullptr, StopInterceptors, nullptr, nullptr, nullptr, napi_default, nullptr}, | ||||||
| 214 | + {"RemoveAllInterceptors", nullptr, RemoveAllInterceptors, nullptr, nullptr, nullptr, napi_default, nullptr}, | ||||||
| 215 | + }; | ||||||
| 216 | + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); | ||||||
| 217 | + return exports; | ||||||
| 218 | + } | ||||||
| 219 | + EXTERN_C_END | ||||||
| 220 | + ``` | ||||||
O 当前建议代码无改动
![]() ![]() | |||||||
| 221 | + | ||||||
| 222 | +3. 将上一步中初始化成功的对象通过`RegisterEntryModule`函数,使用`napi_module_register`函数将模块注册到Node.js中。 | ||||||
| 223 | + | ||||||
| 224 | + <!-- @[HttpInterceptor_napi_module](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/NetWork_Kit/NetWorkKit_Datatransmission/HTTP_interceptor_C/entry/src/main/cpp/napi_init.cpp) --> | ||||||
| 225 | + | ||||||
| 226 | + ``` C++ | ||||||
| 227 | + static napi_module demoModule = { | ||||||
| 228 | + .nm_version = 1, | ||||||
| 229 | + .nm_flags = 0, | ||||||
| 230 | + .nm_filename = nullptr, | ||||||
| 231 | + .nm_register_func = Init, | ||||||
| 232 | + .nm_modname = "entry", | ||||||
| 233 | + .nm_priv = ((void *)0), | ||||||
| 234 | + .reserved = {0}, | ||||||
| 235 | + }; | ||||||
| 236 | + | ||||||
| 237 | + extern "C" __attribute__((constructor)) void RegisterEntryModule(void) | ||||||
| 238 | + { | ||||||
| 239 | + napi_module_register(&demoModule); | ||||||
| 240 | + } | ||||||
| 241 | + ``` | ||||||
O 当前建议代码无改动
![]() ![]() | |||||||
| 242 | + | ||||||
| 243 | +4. 在工程的Index.d.ts文件中定义函数的类型。 | ||||||
| 244 | + | ||||||
| 245 | + <!-- @[HttpInterceptor_defining_function_types](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/NetWork_Kit/NetWorkKit_Datatransmission/HTTP_interceptor_C/entry/src/main/cpp/types/libentry/Index.d.ts) --> | ||||||
| 246 | + | ||||||
| 247 | + ``` TypeScript | ||||||
| 248 | + export const AddResponseInterceptor: () => number; | ||||||
| 249 | + export const RemoveInterceptor: () => number; | ||||||
| 250 | + export const StartInterceptors: () => number; | ||||||
| 251 | + export const StopInterceptors: () => number; | ||||||
| 252 | + export const RemoveAllInterceptors: () => number; | ||||||
| 253 | + ``` | ||||||
O 当前建议代码无改动
![]() ![]() | |||||||
| 254 | + | ||||||
| 255 | +5. 在Index.ets文件中对上述封装好的接口进行调用。 | ||||||
| 256 | + | ||||||
| 257 | + <!-- @[HttpInterceptor_C_full_example](https://gitcode.com/openharmony/applications_app_samples/blob/master/code/DocsSample/NetWork_Kit/NetWorkKit_Datatransmission/HTTP_interceptor_C/entry/src/main/ets/pages/Index.ets) --> | ||||||
| 258 | + | ||||||
| 259 | + ``` TypeScript | ||||||
| 260 | + import { hilog } from '@kit.PerformanceAnalysisKit'; | ||||||
| 261 | + import httpInterceptor from 'libentry.so'; | ||||||
| 262 | + import { http } from '@kit.NetworkKit'; | ||||||
| 263 | + | ||||||
| 264 | + const LOG_TAG: string = 'HttpInterceptorDemo'; | ||||||
| 265 | + const HTTP_URL_BAIDU: string = "http://www.baidu.com"; | ||||||
| 266 | + | ||||||
| 267 | + @Entry | ||||||
| 268 | + @Component | ||||||
| 269 | + struct Index { | ||||||
| 270 | + @State message: string = 'HTTP Interceptor Demo'; | ||||||
| 271 | + | ||||||
| 272 | + build() { | ||||||
| 273 | + Navigation() { | ||||||
| 274 | + Column() { | ||||||
| 275 | + Text(this.message) | ||||||
| 276 | + .fontSize(20) | ||||||
| 277 | + .margin({ bottom: 20 }) | ||||||
| 278 | + | ||||||
| 279 | + Column({ | ||||||
| 280 | + space: 12 | ||||||
| 281 | + }) { | ||||||
| 282 | + Button('Add Response Interceptor') | ||||||
| 283 | + .id('AddInterceptor') | ||||||
| 284 | + .onClick(() => { | ||||||
| 285 | + let ret = httpInterceptor.AddResponseInterceptor(); | ||||||
| 286 | + hilog.info(0x0000, LOG_TAG, `AddResponseInterceptor ret: ${ret}`); | ||||||
| 287 | + }) | ||||||
| 288 | + | ||||||
| 289 | + Button('Start Interceptors') | ||||||
| 290 | + .id('StartInterceptors') | ||||||
| 291 | + .onClick(() => { | ||||||
| 292 | + let ret = httpInterceptor.StartInterceptors(); | ||||||
| 293 | + hilog.info(0x0000, LOG_TAG, `StartInterceptors ret: ${ret}`); | ||||||
| 294 | + }) | ||||||
| 295 | + | ||||||
| 296 | + Button('Send HTTP Request') | ||||||
| 297 | + .id('networkRequest') | ||||||
| 298 | + .onClick(() => { | ||||||
| 299 | + let httpRequest: http.HttpRequest = http.createHttp(); | ||||||
| 300 | + let options: http.HttpRequestOptions = { | ||||||
| 301 | + method: http.RequestMethod.POST, | ||||||
| 302 | + }; | ||||||
| 303 | + httpRequest.request(HTTP_URL_BAIDU, options, (err: BusinessError, res: http.HttpResponse) => { | ||||||
| 304 | + if (err) { | ||||||
| 305 | + hilog.info(0x0000, LOG_TAG, `request fail, error code: ${err.code}, msg: ${err.message}`); | ||||||
| 306 | + httpRequest.destroy(); | ||||||
| 307 | + } else { | ||||||
| 308 | + hilog.info(0x0000, LOG_TAG, `res:${JSON.stringify(res)}`); | ||||||
| 309 | + httpRequest.destroy(); | ||||||
| 310 | + } | ||||||
| 311 | + }); | ||||||
| 312 | + }) | ||||||
| 313 | + | ||||||
| 314 | + Button('Stop Interceptors') | ||||||
| 315 | + .id('StopInterceptors') | ||||||
| 316 | + .onClick(() => { | ||||||
| 317 | + let ret = httpInterceptor.StopInterceptors(); | ||||||
| 318 | + hilog.info(0x0000, LOG_TAG, `StopInterceptors ret: ${ret}`); | ||||||
| 319 | + }) | ||||||
| 320 | + | ||||||
| 321 | + Button('Remove Interceptor') | ||||||
| 322 | + .id('RemoveInterceptor') | ||||||
| 323 | + .onClick(() => { | ||||||
| 324 | + let ret = httpInterceptor.RemoveInterceptor(); | ||||||
| 325 | + hilog.info(0x0000, LOG_TAG, `RemoveInterceptor ret: ${ret}`); | ||||||
| 326 | + }) | ||||||
| 327 | + | ||||||
| 328 | + Button('Remove All Interceptors') | ||||||
| 329 | + .id('RemoveAllInterceptors') | ||||||
| 330 | + .onClick(() => { | ||||||
| 331 | + let ret = httpInterceptor.RemoveAllInterceptors(); | ||||||
| 332 | + hilog.info(0x0000, LOG_TAG, `RemoveAllInterceptors ret: ${ret}`); | ||||||
| 333 | + }) | ||||||
| 334 | + } | ||||||
| 335 | + } | ||||||
| 336 | + .padding(20) | ||||||
| 337 | + } | ||||||
| 338 | + } | ||||||
| 339 | + } | ||||||
| 340 | + ``` | ||||||
O 当前建议代码无改动
![]() ![]() | |||||||
| 341 | + | ||||||
| 342 | +6. 配置`CMakeLists.txt`,本模块需要用到的共享库是`libhttp_interceptor.so`,在工程自动生成的`CMakeLists.txt`中的`target_link_libraries`中添加此共享库。 | ||||||
| 343 | + | ||||||
| 344 | + 注意:如图所示,在`add_library`中的`entry`是工程自动生成的`module name`,若要做修改,需和步骤 3 中`.nm_modname`保持一致。 | ||||||
| 345 | + | ||||||
| 346 | +  | ||||||
| 347 | + | ||||||
| 348 | +7. 调用HTTP全局拦截器C API接口要求应用拥有`ohos.permission.INTERNET`权限,在`module.json5`中的`requestPermissions`项添加该权限。 | ||||||
C API 和NAPI有啥区别,叫法要统一 ![]() ![]() | |||||||
| 349 | + | ||||||
| 350 | +完成上述步骤后,工程搭建已全部完成,后续可连接设备运行工程并查看日志。 | ||||||
| 351 | + | ||||||
| 352 | +## 测试步骤 | ||||||
| 353 | + | ||||||
| 354 | +1. 连接设备,使用DevEco Studio打开搭建好的工程。 | ||||||
| 355 | + | ||||||
| 356 | +2. 运行工程,设备上会弹出以下图片所示界面。 | ||||||
| 357 | + | ||||||
| 358 | + | ||||||
| 359 | + | ||||||
| 360 | + - 点击`Add Response Interceptor`按钮,添加一个HTTP全局只读响应拦截器。 | ||||||
| 361 | + | ||||||
| 362 | + | ||||||
| 363 | + | ||||||
| 364 | + - 点击`Start Interceptors`按钮,启用组ID为1的所有拦截器。 | ||||||
| 365 | + | ||||||
| 366 | + | ||||||
| 367 | + | ||||||
| 368 | + - 点击`Send HTTP Request`按钮,拦截器会捕获响应并打印相关信息到日志。 | ||||||
| 369 | + | ||||||
| 370 | + | ||||||
| 371 | + | ||||||
| 372 | + - 点击`Stop Interceptors`按钮,停用组ID为1的所有拦截器。 | ||||||
| 373 | + | ||||||
| 374 | + | ||||||
| 375 | + | ||||||
| 376 | + - 点击`Remove Interceptor`按钮,移除之前添加的拦截器。 | ||||||
| 377 | + | ||||||
| 378 | + | ||||||
| 379 | + | ||||||
| 380 | + - 点击`Remove All Interceptors`按钮,删除组ID为1的所有拦截器。 | ||||||
| 381 | + | ||||||
| 382 | + | ||||||
| 383 | + | ||||||
| 384 | +## 相关实例 | ||||||
| 385 | + | ||||||
| 386 | +针对HTTP全局拦截器的开发,有以下相关实例可供参考: | ||||||
| 387 | + | ||||||
| 388 | +- [HTTP全局拦截器(C/C++)](https://gitcode.com/openharmony/applications_app_samples/tree/master/code/DocsSample/NetWork_Kit/NetWorkKit_Datatransmission/HTTP_interceptor_C) | ||||||
| @@ -52,7 +52,8 @@ int32_t OH_Http_AddReadOnlyInterceptor(struct OH_Http_Interceptor *interceptor) | |||
| 52 | 52 | ||
| 53 | - 当前仅支持只读响应(OH_STAGE_RESPONSE)拦截器。 | 53 | - 当前仅支持只读响应(OH_STAGE_RESPONSE)拦截器。 |
| 54 | - 拦截器一旦添加,将持续生效,直至开发者显式移除。 | 54 | - 拦截器一旦添加,将持续生效,直至开发者显式移除。 |
| 55 | -- 必须调用 `OH_Http_RemoveInterceptor` 移除单个拦截器,或调用 `OH_Http_RemoveAllInterceptors` 移除整组拦截器以释放资源。 | 55 | +- 必须调用[OH_Http_RemoveInterceptor](#oh_http_removeinterceptor)移除单个拦截器,或调用[OH_Http_RemoveAllInterceptors](#oh_http_removeallinterceptors)移除整组拦截器以释放资源。 |
| 56 | +- 拦截器[OH_Http_Interceptor](capi-netstack-http-interceptor.md)的成员变量`enabled`为0,需要调用[OH_Http_StartAllInterceptors](#oh_http_startallinterceptors)启动拦截器。 | ||
A ❌疑似检测出格式问题 错误原因:代码变量名应该使用反引号或代码标记,而不是直接使用。 建议修改为: 当前建议代码无改动
💡 温馨提示:
请审视上述建议的准确性。若您认为检测结果不合理或属于误报,请通过 反馈通道 告知我们。您的反馈对于提升 AI 检测精度的意义重大,我们将据此持续改进此工具 ![]() ![]() | |||
| 56 | 57 | ||
| 57 | **系统能力:** SystemCapability.Communication.NetStack | 58 | **系统能力:** SystemCapability.Communication.NetStack |
| 58 | 59 | ||
| @@ -68,7 +69,6 @@ int32_t OH_Http_AddReadOnlyInterceptor(struct OH_Http_Interceptor *interceptor) | |||
| 68 | | -- | -- | | 69 | | -- | -- | |
| 69 | | int32_t | 返回值为0表示执行成功;返回值为201表示权限被拒绝;返回值为401表示参数错误(如指针为nullptr,或不支持所添加的拦截器类型)。详细错误码请参考[OH_HTTP_RESULT_OK](capi-net-http-type-h.md#http_errcode)、[OH_HTTP_PERMISSION_DENIED](capi-net-http-type-h.md#http_errcode)和[OH_HTTP_PARAMETER_ERROR](capi-net-http-type-h.md#http_errcode)。 | | 70 | | int32_t | 返回值为0表示执行成功;返回值为201表示权限被拒绝;返回值为401表示参数错误(如指针为nullptr,或不支持所添加的拦截器类型)。详细错误码请参考[OH_HTTP_RESULT_OK](capi-net-http-type-h.md#http_errcode)、[OH_HTTP_PERMISSION_DENIED](capi-net-http-type-h.md#http_errcode)和[OH_HTTP_PARAMETER_ERROR](capi-net-http-type-h.md#http_errcode)。 | |
| 70 | 71 | ||
| 71 | - | ||
| 72 | ### OH_Http_RemoveInterceptor() | 72 | ### OH_Http_RemoveInterceptor() |
| 73 | 73 | ||
| 74 | ```c | 74 | ```c |
| @@ -128,6 +128,8 @@ int32_t OH_Http_StartAllInterceptors(int32_t groupId) | |||
| 128 | 128 | ||
| 129 | 启用指定组ID的所有HTTP拦截器。 | 129 | 启用指定组ID的所有HTTP拦截器。 |
| 130 | 130 | ||
| 131 | +- 调用[OH_Http_StopAllInterceptors](#oh_http_stopallinterceptors)停止拦截器。 | ||
| 132 | + | ||
| 131 | **系统能力:** SystemCapability.Communication.NetStack | 133 | **系统能力:** SystemCapability.Communication.NetStack |
| 132 | 134 | ||
| 133 | **起始版本:** 24 | 135 | **起始版本:** 24 |
| @@ -151,6 +153,8 @@ int32_t OH_Http_StopAllInterceptors(int32_t groupId) | |||
| 151 | 153 | ||
| 152 | 停用指定组ID的所有HTTP拦截器。 | 154 | 停用指定组ID的所有HTTP拦截器。 |
| 153 | 155 | ||
| 156 | +- 调用[OH_Http_StartAllInterceptors](#oh_http_startallinterceptors)重新启用拦截器。 | ||
| 157 | + | ||
| 154 | **系统能力:** SystemCapability.Communication.NetStack | 158 | **系统能力:** SystemCapability.Communication.NetStack |
| 155 | 159 | ||
| 156 | **起始版本:** 24 | 160 | **起始版本:** 24 |
| @@ -35,4 +35,4 @@ typedef struct OH_Http_Interceptor { | |||
| 35 | | OH_Interceptor_Stage stage | 拦截器的执行阶段,详情请参考[OH_Interceptor_Stage](capi-net-http-interceptor-type-h.md#oh_interceptor_stage) 枚举定义。 | | 35 | | OH_Interceptor_Stage stage | 拦截器的执行阶段,详情请参考[OH_Interceptor_Stage](capi-net-http-interceptor-type-h.md#oh_interceptor_stage) 枚举定义。 | |
| 36 | | OH_Interceptor_Type type | 拦截器的类型,详情请参考[OH_Interceptor_Type](capi-net-http-interceptor-type-h.md#oh_interceptor_type) 枚举定义。 | | 36 | | OH_Interceptor_Type type | 拦截器的类型,详情请参考[OH_Interceptor_Type](capi-net-http-interceptor-type-h.md#oh_interceptor_type) 枚举定义。 | |
| 37 | | OH_Http_InterceptorHandler handler | 拦截器处理函数,详情请参考[OH_Http_InterceptorHandler](capi-net-http-interceptor-type-h.md#oh_http_interceptorhandler) 函数指针定义。 | | 37 | | OH_Http_InterceptorHandler handler | 拦截器处理函数,详情请参考[OH_Http_InterceptorHandler](capi-net-http-interceptor-type-h.md#oh_http_interceptorhandler) 函数指针定义。 | |
| 38 | -| int32_t enabled | 拦截器的启用状态。 | | 38 | +| int32_t enabled | 拦截器的启用状态。0代表未启用,非0代表启用。 | |


新增文档要在readme里加,readme是目录