• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Customizing Page Request Responses
2
3
4The **Web** component supports customization of the response to intercepted page requests. You can call [onInterceptRequest()](../reference/arkui-ts/ts-basic-components-web.md#oninterceptrequest9) to customize web page responses, file resource responses, etc.
5
6
7When a resource loading request is initiated on a web page, the application layer will receive the request. The application layer then constructs a local resource response and sends it to the web kernel. On receiving the response, the web kernel parses the response and loads page resources accordingly.
8
9
10In the following example, the **Web** component intercepts the web page request **https://www.intercept.com/test.html** and constructs a custom response in the application code.
11
12
13- Code of the **example.html** page:
14
15  ```html
16  <!DOCTYPE html>
17  <html>
18  <head>
19      <meta charset="utf-8">
20      <title>example</title>
21  </head>
22  <body>
23  <!-- Page resource request ->
24  <a href="https://www.intercept.com/test.html">intercept test!</a>
25  </body>
26  </html>
27  ```
28
29- Application code:
30
31  ```ts
32  // xxx.ets
33  import web_webview from '@ohos.web.webview';
34
35  @Entry
36  @Component
37  struct WebComponent {
38    controller: web_webview.WebviewController = new web_webview.WebviewController()
39    responseResource: WebResourceResponse = new WebResourceResponse()
40    // Customize a response.
41    @State webData: string = '<!DOCTYPE html>\n' +
42    '<html>\n'+
43    '<head>\n'+
44    '<title>intercept test</title>\n'+
45    '</head>\n'+
46    '<body>\n'+
47    '<h1>intercept ok</h1>\n'+
48    '</body>\n'+
49    '</html>'
50    build() {
51      Column() {
52        Web({ src: $rawfile('example.html'), controller: this.controller })
53          .onInterceptRequest((event) => {
54            console.info('url:' + event.request.getRequestUrl());
55            // Intercept the web page request.
56            if (event.request.getRequestUrl() !== 'https://www.intercept.com/test.html') {
57              return null;
58            }
59            // Construct a custom response.
60            this.responseResource.setResponseData(this.webData);
61            this.responseResource.setResponseEncoding('utf-8');
62            this.responseResource.setResponseMimeType('text/html');
63            this.responseResource.setResponseCode(200);
64            this.responseResource.setReasonMessage('OK');
65            return this.responseResource;
66          })
67      }
68    }
69  }
70  ```
71