1# Class (ProxyController) 2<!--Kit: ArkWeb--> 3<!--Subsystem: Web--> 4<!--Owner: @aohui--> 5<!--Designer: @yaomingliu--> 6<!--Tester: @ghiker--> 7<!--Adviser: @HelloCrease--> 8 9此类用于为应用程序设置代理。 10 11> **说明:** 12> 13> - 本模块首批接口从API version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 14> 15> - 本Class首批接口从API version 15开始支持。 16> 17> - 示例效果请以真机运行为准,当前DevEco Studio预览器不支持。 18 19## 导入模块 20 21```ts 22import { webview } from '@kit.ArkWeb'; 23``` 24 25## applyProxyOverride<sup>15+</sup> 26 27static applyProxyOverride(proxyConfig: ProxyConfig, callback: OnProxyConfigChangeCallback): void 28 29设置应用中所有Web使用的代理配置,与[insertBypassRule](./arkts-apis-webview-ProxyConfig.md#insertbypassrule15)中插入的bypass规则匹配的URL将不会使用代理,而是直接向URL指定的源地址发起请求。代理设置成功后,不保证网络连接后会立即使用新的代理设置,在加载页面之前请等待监听器触发,这个监听器将在UI线程上被调用。 30 31**系统能力:** SystemCapability.Web.Webview.Core 32 33**参数:** 34 35| 参数名 | 类型 | 必填 | 说明 | 36| ---------------| ------- | ---- | ------------- | 37| proxyConfig | [ProxyConfig](./arkts-apis-webview-ProxyConfig.md) | 是 | 对代理的配置。 | 38| callback | [OnProxyConfigChangeCallback](./arkts-apis-webview-t.md#onproxyconfigchangecallback15) | 是 | 代理设置成功的回调。 | 39 40**错误码:** 41 42以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)说明文档。 43 44| 错误码ID | 错误信息 | 45| -------- | ------------------------------------- | 46| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 47 48**示例:** 49 50完整示例代码参考[removeProxyOverride](./arkts-apis-webview-ProxyController.md#removeproxyoverride15)。 51 52## removeProxyOverride<sup>15+</sup> 53 54static removeProxyOverride(callback: OnProxyConfigChangeCallback): void 55 56移除代理配置。移除代理配置后,不保证网络连接后会立即使用新的代理设置,在加载页面之前等待监听器,这个监听器将在UI线程上被调用。 57 58**系统能力:** SystemCapability.Web.Webview.Core 59 60**参数:** 61 62| 参数名 | 类型 | 必填 | 说明 | 63| ---------------| ------- | ---- | ------------- | 64| callback | [OnProxyConfigChangeCallback](./arkts-apis-webview-t.md#onproxyconfigchangecallback15) | 是 | 代理设置成功的回调。 | 65 66**错误码:** 67 68以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)说明文档。 69 70| 错误码ID | 错误信息 | 71| -------- | ------------------------------------- | 72| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 73 74**示例:** 75 76```ts 77// xxx.ets 78import { webview } from '@kit.ArkWeb'; 79import { BusinessError } from '@kit.BasicServicesKit'; 80 81@Entry 82@Component 83struct WebComponent { 84 controller: webview.WebviewController = new webview.WebviewController(); 85 proxyRules: webview.ProxyRule[] = []; 86 87 build() { 88 Row() { 89 Column() { 90 Button("applyProxyOverride").onClick(()=>{ 91 let proxyConfig:webview.ProxyConfig = new webview.ProxyConfig(); 92 //优先使用第一个代理配置https://proxy.XXX.com 93 //代理失败后会回落到直连服务器insertDirectRule 94 try { 95 proxyConfig.insertProxyRule("https://proxy.XXX.com", webview.ProxySchemeFilter.MATCH_ALL_SCHEMES); 96 } catch (error) { 97 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 98 } 99 try { 100 proxyConfig.insertDirectRule(webview.ProxySchemeFilter.MATCH_HTTP); 101 } catch (error) { 102 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 103 } 104 try { 105 proxyConfig.insertBypassRule("*.example.com"); 106 } catch (error) { 107 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 108 } 109 proxyConfig.clearImplicitRules(); 110 proxyConfig.bypassHostnamesWithoutPeriod(); 111 try { 112 proxyConfig.enableReverseBypass(true); 113 } catch (error) { 114 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 115 } 116 let bypassRules = proxyConfig.getBypassRules(); 117 for (let i = 0; i < bypassRules.length; i++) { 118 console.info("bypassRules: " + bypassRules[i]); 119 } 120 this.proxyRules = proxyConfig.getProxyRules(); 121 for (let i = 0; i < this.proxyRules.length; i++) { 122 console.info("SchemeFiletr: " + this.proxyRules[i].getSchemeFilter()); 123 console.info("Url: " + this.proxyRules[i].getUrl()); 124 } 125 let isReverseBypassRule = proxyConfig.isReverseBypassEnabled(); 126 console.info("isReverseBypassRules: " + isReverseBypassRule); 127 try { 128 webview.ProxyController.applyProxyOverride(proxyConfig, () => { 129 console.info("PROXYCONTROLLER proxy changed"); 130 }); 131 } catch (error) { 132 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 133 } 134 }) 135 Button("loadUrl-https").onClick(()=>{ 136 this.controller.loadUrl("https://www.example.com") 137 }) 138 Button("loadUrl-http").onClick(()=>{ 139 this.controller.loadUrl("http://www.example.com") 140 }) 141 Button("removeProxyOverride").onClick(()=>{ 142 try { 143 webview.ProxyController.removeProxyOverride(() => { 144 console.info("PROXYCONTROLLER proxy changed"); 145 }); 146 } catch (error) { 147 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 148 } 149 }) 150 Web({ src: 'www.example.com', controller: this.controller}) 151 } 152 .width('100%') 153 } 154 .height('100%') 155 } 156} 157 158```