http_interceptor.h

Overview

Defines the APIs of the HTTP global interceptor module. With interceptors, you can monitor HTTP mobile data and implement the logging feature.

  • Restrictions on read-only interceptors: Do not modify the request and response content or release the pointer in a read-only interceptor. Even if you modify the content, the modification will not take effect on the request, but may affect the subsequent read-only interceptors' reading of the data packet content. In addition, setting OH_ABORT in a read-only interceptor will not take effect.
  • Supported components: Currently, the global interceptor supports @ohos.net.http, net_http.h, rcp and @ohos.request.cacheDownload.
  • Trigger conditions:
    • The intermediate process of automatic redirection is not intercepted (only the final response is exposed).
    • The response when a cache hit occurs is not intercepted (because there is no actual network request).

Header file: <network/netstack/http_interceptor.h>

Library: libhttp_interceptor.so

System capability: SystemCapability.Communication.NetStack

Since: 24

Related module: netstack

Summary

Function

Name Description
int32_t OH_Http_AddReadOnlyInterceptor(struct OH_Http_Interceptor *interceptor) Adds a global read-only HTTP interceptor.
int32_t OH_Http_RemoveInterceptor(struct OH_Http_Interceptor *interceptor) Removes a specified global HTTP interceptor.
int32_t OH_Http_RemoveAllInterceptors(int32_t groupId) Removes all HTTP interceptors of a specified group ID.
int32_t OH_Http_StartAllInterceptors(int32_t groupId) Enables all HTTP interceptors of a specified group ID.
int32_t OH_Http_StopAllInterceptors(int32_t groupId) Disables all HTTP interceptors of a specified group ID.

Function Description

OH_Http_AddReadOnlyInterceptor()

int32_t OH_Http_AddReadOnlyInterceptor(struct OH_Http_Interceptor *interceptor)

Description

Adds a global read-only HTTP interceptor.

System capability: SystemCapability.Communication.NetStack

Since: 24

Parameters

Name Description
struct OH_Http_Interceptor *interceptor Pointer to the OH_Http_Interceptor struct to be added.

Returns

Type Description
int32_t If the return value is 0, the execution is successful. If the return value is 201, the permission is denied. If the return value is 401, the parameter is incorrect (for example, the pointer is nullptr or the added interceptor type is not supported). For details about the error codes, see OH_HTTP_RESULT_OK, OH_HTTP_PERMISSION_DENIED, and OH_HTTP_PARAMETER_ERROR.

OH_Http_RemoveInterceptor()

int32_t OH_Http_RemoveInterceptor(struct OH_Http_Interceptor *interceptor)

Description

Removes a specified global HTTP interceptor.

System capability: SystemCapability.Communication.NetStack

Since: 24

Parameters

Name Description
struct OH_Http_Interceptor *interceptor Pointer to the OH_Http_Interceptor struct to be removed.

Returns

Type Description
int32_t If the return value is 0, the execution is successful. If the return value is 201, the permission is denied. If the return value is 401, the parameter is incorrect (for example, the pointer is nullptr). For details about the error codes, see OH_HTTP_RESULT_OK, OH_HTTP_PERMISSION_DENIED, and OH_HTTP_PARAMETER_ERROR.

OH_Http_RemoveAllInterceptors()

int32_t OH_Http_RemoveAllInterceptors(int32_t groupId)

Description

Removes all HTTP interceptors of a specified group ID.

  • The group ID is allocated and managed by the application when the interceptors are created.
  • If multiple modules in the application use interceptors, you must properly set group IDs to avoid conflicts.
  • If a group ID conflict occurs, interceptors of other modules may be deleted unexpectedly when this function is called.

System capability: SystemCapability.Communication.NetStack

Since: 24

Parameters

Name Description
int32_t groupId Interceptor group ID.

Returns

Type Description
int32_t If the return value is 0, the execution is successful. If the return value is 201, the permission is denied. For details about the error codes, see OH_HTTP_RESULT_OK and OH_HTTP_PERMISSION_DENIED.

OH_Http_StartAllInterceptors()

int32_t OH_Http_StartAllInterceptors(int32_t groupId)

Description

Enables all HTTP interceptors of a specified group ID.

System capability: SystemCapability.Communication.NetStack

Since: 24

Parameters

Name Description
int32_t groupId Interceptor group ID.

Returns

Type Description
int32_t If the return value is 0, the execution is successful. If the return value is 201, the permission is denied. For details about the error codes, see OH_HTTP_RESULT_OK and OH_HTTP_PERMISSION_DENIED.

OH_Http_StopAllInterceptors()

int32_t OH_Http_StopAllInterceptors(int32_t groupId)

Description

Disables all HTTP interceptors of a specified group ID.

System capability: SystemCapability.Communication.NetStack

Since: 24

Parameters

Name Description
int32_t groupId Interceptor group ID.

Returns

Type Description
int32_t If the return value is 0, the execution is successful. If the return value is 201, the permission is denied. For details about the error codes, see OH_HTTP_RESULT_OK and OH_HTTP_PERMISSION_DENIED.