1# Class (WebDataBase) 2<!--Kit: ArkWeb--> 3<!--Subsystem: Web--> 4<!--Owner: @yuzhouhang1--> 5<!--Designer: @handyohos--> 6<!--Tester: @ghiker--> 7<!--Adviser: @HelloCrease--> 8 9Web组件数据库管理对象。 10 11> **说明:** 12> 13> - 本模块首批接口从API version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 14> 15> - 本Class首批接口从API version 9开始支持。 16> 17> - 示例效果请以真机运行为准,当前DevEco Studio预览器不支持。 18> 19> - 目前调用WebDataBase下的方法,都需要先加载Web组件。 20 21## 导入模块 22 23```ts 24import { webview } from '@kit.ArkWeb'; 25``` 26 27## getHttpAuthCredentials 28 29static getHttpAuthCredentials(host: string, realm: string): Array\<string> 30 31检索给定主机和域的HTTP身份验证凭据,该方法为同步方法。 32 33**系统能力:** SystemCapability.Web.Webview.Core 34 35**参数:** 36 37| 参数名 | 类型 | 必填 | 说明 | 38| ------ | ------ | ---- | ---------------------------- | 39| host | string | 是 | HTTP身份验证凭据应用的主机。 | 40| realm | string | 是 | HTTP身份验证凭据应用的域。 | 41 42**返回值:** 43 44| 类型 | 说明 | 45| ----- | -------------------------------------------- | 46| Array\<string> | 包含用户名和密码的组数,检索失败返回空数组。 | 47 48**错误码:** 49 50以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。 51 52| 错误码ID | 错误信息 | 53| -------- | ------------------------------------------------------ | 54| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 55 56**示例:** 57 58```ts 59// xxx.ets 60import { webview } from '@kit.ArkWeb'; 61import { BusinessError } from '@kit.BasicServicesKit'; 62 63@Entry 64@Component 65struct WebComponent { 66 controller: webview.WebviewController = new webview.WebviewController(); 67 host: string = "www.spincast.org"; 68 realm: string = "protected example"; 69 username_password: string[] = []; 70 71 build() { 72 Column() { 73 Button('getHttpAuthCredentials') 74 .onClick(() => { 75 try { 76 this.username_password = webview.WebDataBase.getHttpAuthCredentials(this.host, this.realm); 77 console.info('num: ' + this.username_password.length); 78 } catch (error) { 79 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 80 } 81 }) 82 Web({ src: 'www.example.com', controller: this.controller }) 83 } 84 } 85} 86``` 87 88## saveHttpAuthCredentials 89 90static saveHttpAuthCredentials(host: string, realm: string, username: string, password: string): void 91 92保存给定主机和域的HTTP身份验证凭据,该方法为同步方法。 93 94**系统能力:** SystemCapability.Web.Webview.Core 95 96**参数:** 97 98| 参数名 | 类型 | 必填 | 说明 | 99| -------- | ------ | ---- | ---------------------------- | 100| host | string | 是 | HTTP身份验证凭据应用的主机。 | 101| realm | string | 是 | HTTP身份验证凭据应用的域。 | 102| username | string | 是 | 用户名。 | 103| password | string | 是 | 密码。 | 104 105**错误码:** 106 107以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。 108 109| 错误码ID | 错误信息 | 110| -------- | ------------------------------------------------------ | 111| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 112 113**示例:** 114 115```ts 116// xxx.ets 117import { webview } from '@kit.ArkWeb'; 118import { BusinessError } from '@kit.BasicServicesKit'; 119 120@Entry 121@Component 122struct WebComponent { 123 controller: webview.WebviewController = new webview.WebviewController(); 124 host: string = "www.spincast.org"; 125 realm: string = "protected example"; 126 127 build() { 128 Column() { 129 Button('saveHttpAuthCredentials') 130 .onClick(() => { 131 try { 132 webview.WebDataBase.saveHttpAuthCredentials(this.host, this.realm, "Stromgol", "Laroche"); 133 } catch (error) { 134 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 135 } 136 }) 137 Web({ src: 'www.example.com', controller: this.controller }) 138 } 139 } 140} 141``` 142 143## existHttpAuthCredentials 144 145static existHttpAuthCredentials(): boolean 146 147判断是否存在任何已保存的HTTP身份验证凭据,该方法为同步方法。 148 149**系统能力:** SystemCapability.Web.Webview.Core 150 151**返回值:** 152 153| 类型 | 说明 | 154| ------- | ------------------------------------------------------------ | 155| boolean | 是否存在任何已保存的HTTP身份验证凭据。<br>存在返回true,不存在返回false。 | 156 157**示例:** 158 159```ts 160// xxx.ets 161import { webview } from '@kit.ArkWeb'; 162import { BusinessError } from '@kit.BasicServicesKit'; 163 164@Entry 165@Component 166struct WebComponent { 167 controller: webview.WebviewController = new webview.WebviewController(); 168 169 build() { 170 Column() { 171 Button('existHttpAuthCredentials') 172 .onClick(() => { 173 try { 174 let result = webview.WebDataBase.existHttpAuthCredentials(); 175 } catch (error) { 176 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 177 } 178 }) 179 Web({ src: 'www.example.com', controller: this.controller }) 180 } 181 } 182} 183``` 184 185## deleteHttpAuthCredentials 186 187static deleteHttpAuthCredentials(): void 188 189清除所有已保存的HTTP身份验证凭据,该方法为同步方法。 190 191**系统能力:** SystemCapability.Web.Webview.Core 192 193**示例:** 194 195```ts 196// xxx.ets 197import { webview } from '@kit.ArkWeb'; 198import { BusinessError } from '@kit.BasicServicesKit'; 199 200@Entry 201@Component 202struct WebComponent { 203 controller: webview.WebviewController = new webview.WebviewController(); 204 205 build() { 206 Column() { 207 Button('deleteHttpAuthCredentials') 208 .onClick(() => { 209 try { 210 webview.WebDataBase.deleteHttpAuthCredentials(); 211 } catch (error) { 212 console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`); 213 } 214 }) 215 Web({ src: 'www.example.com', controller: this.controller }) 216 } 217 } 218} 219```