• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class (ProxyController)
2
3Implements a **ProxyController** object to set a proxy for an application.
4
5> **NOTE**
6>
7> - The initial APIs of this module are supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version.
8>
9> - The initial APIs of this class are supported since API version 15.
10>
11> - You can preview how this component looks on a real device, but not in DevEco Studio Previewer.
12
13## Modules to Import
14
15```ts
16import { webview } from '@kit.ArkWeb';
17```
18
19## applyProxyOverride<sup>15+</sup>
20
21static applyProxyOverride(proxyConfig: ProxyConfig, callback: OnProxyConfigChangeCallback): void
22
23Sets the proxy used by all web pages in an application. URLs that match the bypass rule inserted through [insertBypassRule](./arkts-apis-webview-ProxyConfig.md#insertbypassrule15) do not use the proxy. Instead, their requests are directly sent to the source addresses specified by the URLs. The new proxy may not be used immediately after the network is connected. Before loading the page, wait for the listener to be triggered in the UI thread.
24
25**System capability**: SystemCapability.Web.Webview.Core
26
27**Parameters**
28
29| Name         | Type    |  Mandatory | Description          |
30| ---------------| ------- | ---- | ------------- |
31| proxyConfig     | [ProxyConfig](./arkts-apis-webview-ProxyConfig.md)  | Yes  | Configuration of the proxy.|
32| callback     | [OnProxyConfigChangeCallback](./arkts-apis-webview-t.md#onproxyconfigchangecallback15)   | Yes  | Callback used when the proxy is successfully set.|
33
34**Error codes**
35
36For details about the following error codes, see [Universal Error Codes](../errorcode-universal.md).
37
38| Error Code| Error Message                             |
39| -------- | ------------------------------------- |
40|  401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
41
42**Example**
43
44For details about the sample code, see [removeProxyOverride](./arkts-apis-webview-ProxyController.md#removeproxyoverride15).
45
46## removeProxyOverride<sup>15+</sup>
47
48static removeProxyOverride(callback: OnProxyConfigChangeCallback): void
49
50Removes the proxy configuration. The new proxy may not be used immediately after the network is connected. Before loading the page, wait for the listener to be triggered in the UI thread.
51
52**System capability**: SystemCapability.Web.Webview.Core
53
54**Parameters**
55
56| Name         | Type    |  Mandatory | Description          |
57| ---------------| ------- | ---- | ------------- |
58| callback     | [OnProxyConfigChangeCallback](./arkts-apis-webview-t.md#onproxyconfigchangecallback15)   | Yes  | Callback used when the proxy is successfully set.|
59
60**Error codes**
61
62For details about the following error codes, see [Universal Error Codes](../errorcode-universal.md).
63
64| Error Code| Error Message                             |
65| -------- | ------------------------------------- |
66|  401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.  |
67
68**Example**
69
70```ts
71// xxx.ets
72import { webview } from '@kit.ArkWeb';
73import { BusinessError } from '@kit.BasicServicesKit';
74
75@Entry
76@Component
77struct WebComponent {
78  controller: webview.WebviewController = new webview.WebviewController();
79  proxyRules: webview.ProxyRule[] = [];
80
81  build() {
82    Row() {
83      Column() {
84        Button("applyProxyOverride").onClick(()=>{
85          let proxyConfig:webview.ProxyConfig = new webview.ProxyConfig();
86          // The first proxy configuration https://proxy.XXX.com is preferentially used.
87          // When the proxy fails, insertDirectRule is used.
88          try {
89            proxyConfig.insertProxyRule("https://proxy.XXX.com", webview.ProxySchemeFilter.MATCH_ALL_SCHEMES);
90          } catch (error) {
91            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
92          }
93          try {
94            proxyConfig.insertDirectRule(webview.ProxySchemeFilter.MATCH_HTTP);
95          } catch (error) {
96            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
97          }
98          try {
99            proxyConfig.insertBypassRule("*.example.com");
100          } catch (error) {
101            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
102          }
103          proxyConfig.clearImplicitRules();
104          proxyConfig.bypassHostnamesWithoutPeriod();
105          try {
106            proxyConfig.enableReverseBypass(true);
107          } catch (error) {
108            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
109          }
110          let bypassRules = proxyConfig.getBypassRules();
111          for (let i = 0; i < bypassRules.length; i++) {
112            console.log("bypassRules: " + bypassRules[i]);
113          }
114          this.proxyRules = proxyConfig.getProxyRules();
115          for (let i = 0; i < this.proxyRules.length; i++) {
116            console.log("SchemeFiletr: " + this.proxyRules[i].getSchemeFilter());
117            console.log("Url: " + this.proxyRules[i].getUrl());
118          }
119          let isReverseBypassRule = proxyConfig.isReverseBypassEnabled();
120          console.log("isReverseBypassRules: " + isReverseBypassRule);
121          try {
122            webview.ProxyController.applyProxyOverride(proxyConfig, () => {
123              console.log("PROXYCONTROLLER proxy changed");
124            });
125          } catch (error) {
126            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
127          }
128        })
129        Button("loadUrl-https").onClick(()=>{
130          this.controller.loadUrl("https://www.example.com")
131        })
132        Button("loadUrl-http").onClick(()=>{
133          this.controller.loadUrl("http://www.example.com")
134        })
135        Button("removeProxyOverride").onClick(()=>{
136          try {
137          webview.ProxyController.removeProxyOverride(() => {
138            console.log("PROXYCONTROLLER proxy changed");
139          });
140          } catch (error) {
141            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
142          }
143        })
144        Web({ src: 'www.example.com', controller: this.controller})
145      }
146      .width('100%')
147    }
148    .height('100%')
149  }
150}
151
152```
153