ArkWeb提供了多种网络请求拦截相关能力,本示例基于onLoadIntercept()、onInterceptRequest()和WebSchemeHandler,实现了页面重定向、链接白名单配置、本地资源替换、自定义资源加载策略和配置公共请求头等典型开发场景。
Access rate limit exceeded. Please log in to continue.
Request Interception Using Web Components
Overview
This sample implements request interception for Web components via onLoadIntercept(), onInterceptRequest(), and WebSchemeHandler (request interceptor). It also provides application cases for typical scenarios such as request redirection, trustlist configuration, local resource replacement, custom resource loading strategy, and common request header configuration, helping you better understand and leverage the request interception capabilities.
Effect
| Request Redirection | Trustlist Configuration | Local Resource Replacement | Custom Resource Loading Strategy | Common Request Header Configuration | |
|---|---|---|---|---|---|
| URL configuration | ![]() |
![]() |
![]() |
![]() |
![]() |
| Web page loading | ![]() |
![]() |
![]() |
![]() |
![]() |
How to Install
To clearly demonstrate the response information when configuring common request headers, this sample sets up a local server. After common request headers are added to the original request, the request is forwarded to the local server, which then returns the request header information as a response to the client. You can refer to the following steps to start the local server:
-
Set up the Node.js environment. The server of this sample is implemented based on Node.js. If you already have a Node.js environment locally, you can skip this step.
-
Check the local Node.js environment. Open the Terminal window in DevEco Studio at the root directory of the sample project and run the node -v command. If the version information is displayed, the Node.js environment is already available.

-
If the Node.js environment is unavailable, download the required version from the Node.js official website and install it.
-
After configuring the environment variables, open Terminal again and run node -v. If the version information is displayed, the installation is successful.
-
-
Configure the local area network (LAN) environment. When configuring common request headers, ensure that the computer running the server code and the test device are connected to the same LAN. You can enable a personal hotspot and connect both the test device and the server computer to this hotspot for testing.
-
Run the server code. Open Terminal in DevEco Studio at the root directory and run the hvigorw startHeaderServer command to start the local server.

-
Connect to the server address. Copy the server URL in Terminal to COMMON_HEADER_REQUEST_URL in the src/main/ets/common/CommonConstants.ets file, save the file, and run the project for testing.

Note: Alternatively, you can directly run the project, manually input the IP address of the computer running the server code in the input box on the common request header configuration page, and send the request. You can read the local network IP in Terminal or run the ipconfig command in the command line tool to view the IP address.

-
To restart the local server, run the hvigorw restartHeaderServer command.
-
To stop the local server, run the hvigorw stopHeaderServer command.
How to Use
-
Go to the Request redirection page, input the original request URL and redirection URL (with default value set), and tap Load web page to send a request. A dialog box will appear for handling the response.
-
Go to the Trustlist configuration page, input the trustlist URL and loading URL (with default value set), and tap Load web page to send a request. If the loading URL is in the trustlist, a dialog box will appear for handling the response. Otherwise, an authorization message will be displayed. After confirmation, you will be redirected to the browser to open the address.
-
Go to the Local resource replacement page, input the request URL (with default value set), and tap Load web page to send a request. If the request URL meets the preset condition below, a local HTML page will be displayed in a dialog box. Otherwise, a dialog box will appear for handling the response.
-
Go to the Custom resource loading strategy page, input the request URL (with default value set), and tap Load web page to send a request. If the device is connected to a Wi-Fi network, the image resource of the request URL will pop up in a dialog box. Otherwise, a local placeholder image will be displayed.
-
Go to the Configure common request header page, input the request URL (with default value set), and tap Load web page to send a request. A dialog box will appear for handling the response.
Project Directory
├──entry
│ └──src
│ └──main
│ ├──ets
│ │ ├──common
│ │ │ ├──utils
│ │ │ │ ├──Logger.ets // Log utility
│ │ │ │ └──RcpRequestForwarder.ets // RCP request forwarding utility
│ │ │ └──CommonConstants.ets // Static constants
│ │ ├──component
│ │ │ ├──NavAreaComponent.ets // Navigation component for jumping to subpages
│ │ │ ├──UrlInputComponent.ets // URL input box component
│ │ │ └──TrustlistDialog.ets // Dialog for redirecting non-trustlisted URLs to browser
│ │ ├──entryability
│ │ │ └──EntryAbility.ets // Application entry
│ │ ├──interceptors
│ │ │ ├──CommonHeaderInterceptor // Interceptor for the common request header
│ │ │ │ ├──model
│ │ │ │ │ └──CommonHeaderModel.ets // Data and processing for the common request header
│ │ │ │ ├──view
│ │ │ │ │ └──CommonHeaderView.ets // UI for the common request header
│ │ │ │ └──viewmodel
│ │ │ │ └──CommonHeaderViewModel.ets // Management of UI and data interaction for the common request header
│ │ │ ├──CustomLoadingStrategyInterceptor // Interceptor for custom resource loading
│ │ │ │ ├──model
│ │ │ │ │ └──CustomLoadingStrategyModel.ets // Data and processing for custom resource loading
│ │ │ │ ├──view
│ │ │ │ │ └──CustomLoadingStrategyView.ets // UI for custom resource loading
│ │ │ │ └──viewmodel
│ │ │ │ └──CustomLoadingStrategyViewModel.ets // Management of UI and data interaction for custom resource loading
│ │ │ ├──LocalResourceInterceptor // Interceptor for local resource replacement
│ │ │ │ ├──model
│ │ │ │ │ └──LocalResourceModel.ets // Data and processing for local resource replacement
│ │ │ │ ├──view
│ │ │ │ │ └──LocalResourceView.ets // UI for local resource replacement
│ │ │ │ └──viewmodel
│ │ │ │ └──LocalResourceViewModel.ets // Management of UI and data interaction for local resource replacement
│ │ │ ├──PageTrustlistInterceptor // Interceptor for trustlist configuration
│ │ │ │ ├──model
│ │ │ │ │ └──PageTrustlistModel.ets // Data and processing for trustlist configuration
│ │ │ │ ├──view
│ │ │ │ │ └──PageTrustlistView.ets // UI for trustlist configuration
│ │ │ │ └──viewmodel
│ │ │ │ └──PageTrustlistViewModel.ets // Management of UI and data interaction for trustlist configuration
│ │ │ └──RedirectRequestInterceptor // Interceptor for request redirection
│ │ │ ├──model
│ │ │ │ └──RedirectRequestModel.ets // Data and processing for request redirection
│ │ │ ├──view
│ │ │ │ └──RedirectRequestView.ets // UI for request redirection
│ │ │ └──viewmodel
│ │ │ └──RedirectRequestViewModel.ets // Management of UI and data interaction for request redirection
│ │ └──pages
│ │ └──Index.ets // Home page
│ └──resources // Static resources
├──LocalServer // Local server
├──scripts
│ └──commandTask.ts // Command file
└──hvigorfile.ts // Build script
How to Implement
- Request redirection: use onLoadIntercept() to intercept the original request URL and use WebviewController.load() to load the redirection URL.
- Trustlist configuration: configure a URL trustlist. Use onLoadIntercept() to intercept untrusted URLs, after which a dialog will prompt to redirect to the browser.
- Local resource replacement: use onInterceptRequest() to intercept URLs matching preset conditions and set the local HTML5 page to WebResourceResponse for response.
- Custom resource loading strategy: use the connect APIs to check whether the current network is Wi-Fi. If the device is not connected to Wi-Fi, use onInterceptRequest() to intercept the URLs of image resources in JPG and PNG formats, and set the local placeholder image to WebResourceResponse for response.
- Common request header configuration: use WebSchemeHandler() to intercept network requests. After adding common request headers, forward the request to the local server via RCP.
Required Permissions
ohos.permission.INTERNET: allows an application to access the Internet.
ohos.permission.GET_NETWORK_INFO: allows an application to obtain network information.
Constraints
-
The sample app is supported only on Bar phones running the standard system.
-
The HarmonyOS version must be HarmonyOS 6.0.0 Release or later.
-
The DevEco Studio version must be DevEco Studio 6.0.0 Release or later.
-
The HarmonyOS SDK version must be HarmonyOS 6.0.0 Release SDK or later.
Introduction
ArkWeb提供了多种网络请求拦截相关能力,本示例基于onLoadIntercept()、onInterceptRequest()和WebSchemeHandler,实现了页面重定向、链接白名单配置、本地资源替换、自定义资源加载策略和配置公共请求头等典型开发场景。
Customize your domain








