WebCrossDomain:基于ArkWeb的Web页面跨域解决方案项目

用户可借助此项目学习和解决Web页面跨域问题,通过ArkWeb拦截器和Cookies管理能力,演示本地资源、远程请求、跨域Cookies设置及自定义协议跨域场景,提供实现思路与代码示例。

Branch2Tags0
FilesLast commitLast update
1 year ago
1 year ago
1 year ago
6 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
4 months ago
4 months ago
1 year ago
1 year ago
1 year ago

Cross-Domain Web Page Access Based on ArkWeb Interceptors and Cookie Management Capabilities

Overview

This sample demonstrates how to use ArkWeb interceptors and cookie management capabilities to implement typical cross-domain web page scenarios, including local resource access, remote requests, cookie configuration, and web page access via a custom protocol. Through this sample, you will understand cross-domain principles and efficiently resolve cross-domain issues in real-world projects.

Preview

Home Page Local Resource Access Page Remote Request Page
Home Page Local Resource Cross-Domain Remote Request Cross-Domain
Cookies Settings Page Custom Protocol Page
Cross-Domain Cookie Setting Custom Protocol Cross-Domain

Procedure

  1. Open the project in DevEco Studio, click Terminal in the root directory, and run the hvigorw startServer command to start the local server.

    Screenshot_terminal

  2. Wait for the compilation to finish.

  3. To restart the local server, run the hvigorw restartServer command.

  4. To stop the local server, run the hvigorw stopServer command.

How to Use

  1. On the local resource access page, tap the button to send a request. The request result will be displayed in the log area.
  2. On the remote request page, tap the button to send a request. The request result will be displayed in the log area.
  3. On the cookies settings page, when the page loads, the system automatically sends a request to domain A, sets cookies, and shows the result in the log area.
  4. Then you can tap the button to obtain cookies of domain A, pass them to domain B, load domain B's page, and send a request to domain B. The request result will be displayed in the log area.
  5. On the custom protocol page, tap the button to send a request. A dialog box will pop up with "Custom protocol has been intercepted".

Project Directory

├── entry/src/main/ets                              
│  ├── common
│  │  ├── Constants.ets                  // Static constants
│  │  ├── HttpProxy.ets                  // Network request proxy
│  │  ├── Logger.ets                     // Log file
│  │  └── PageLoadError.ets              // Web loading error page
│  ├── component                  
│  │  ├── CardItem.ets                   // List item on the home page
│  │  └── Header.ets                     // Header of the details page 
│  ├── entryability  
│  │  └── EntryAbility.ets               // Entry ability
│  ├── pages                                     
│  │  └── Index.ets                      // Home page
│  └── view
│     ├── CookiesSettings.ets            // Cookies settings page
│     ├── CustomProtocol.ets             // Custom protocol page
│     ├── LocalResource.ets              // Local resource access page
│     └── RemoteRequest.ets              // Remote request page
├── entry/src/main/resources             // Static resources
├── LocalVue                             // HTML5 pages
├── LocalServer                          // Local server
└── scripts/commandTask.ts               // Command file

How to Implement

  1. For local resource access across domains, call setPathAllowingUniversalAccess() to configure a path list, enabling access to local files via the file protocol.
  2. For remote requests across domains, set up a WebSchemeHandler interceptor in the WebView to intercept web page requests. Use an RCP request as a cross-domain proxy, and forward it to the target remote server. Since communication between the proxy request and remote server is not restricted by the browser, server responses can be received. However, cross-domain response headers still need to be configured to resolve cross-domain issues when results are sent back to the WebView.
  3. For cookie configuration, use putAcceptCookieEnabled() to allow the WebCookieManager instance to send and receive cookies. Retrieve cookies for domain A with fetchCookiesync(), and set cookies for domain B using configCookiesync(). These cookies will be automatically included in requests sent to domain B.
  4. For web page access via a custom protocol, configure a WebSchemeHandler interceptor in the WebView to intercept requests, call system capabilities, and display a system dialog.

Required Permissions

ohos.permission.INTERNET: allows an application to access the Internet.

Constraints

  1. This sample is only supported on Bar phones running standard systems.
  2. The HarmonyOS version must be HarmonyOS 5.0.5 Release or later.
  3. The DevEco Studio version must be DevEco Studio 5.0.5 Release or later.
  4. The HarmonyOS SDK version must be HarmonyOS 5.0.5 Release SDK or later.

Introduction

用户可借助此项目学习和解决Web页面跨域问题,通过ArkWeb拦截器和Cookies管理能力,演示本地资源、远程请求、跨域Cookies设置及自定义协议跨域场景,提供实现思路与代码示例。

Customize your domain