• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class (WebDataBase)
2
3Implements a **WebDataBase** object.
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 9.
10>
11> - You can preview how this component looks on a real device, but not in DevEco Studio Previewer.
12>
13> - You must load the **Web** component before calling the APIs in **WebDataBase**.
14
15## Modules to Import
16
17```ts
18import { webview } from '@kit.ArkWeb';
19```
20
21## getHttpAuthCredentials
22
23static getHttpAuthCredentials(host: string, realm: string): Array\<string>
24
25Retrieves HTTP authentication credentials for a given host and realm. This API returns the result synchronously.
26
27**System capability**: SystemCapability.Web.Webview.Core
28
29**Parameters**
30
31| Name| Type  | Mandatory| Description                        |
32| ------ | ------ | ---- | ---------------------------- |
33| host   | string | Yes  | Host to which the HTTP authentication credential is applied.|
34| realm  | string | Yes  | Realm to which the HTTP authentication credential is applied.  |
35
36**Return value**
37
38| Type | Description                                        |
39| ----- | -------------------------------------------- |
40| Array\<string> | Array of the matching user names and passwords is returned if the operation is successful; otherwise, an empty array is returned.|
41
42**Error codes**
43
44For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
45
46| Error Code| Error Message                                               |
47| -------- | ------------------------------------------------------ |
48| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
49
50**Example**
51
52```ts
53// xxx.ets
54import { webview } from '@kit.ArkWeb';
55import { BusinessError } from '@kit.BasicServicesKit';
56
57@Entry
58@Component
59struct WebComponent {
60  controller: webview.WebviewController = new webview.WebviewController();
61  host: string = "www.spincast.org";
62  realm: string = "protected example";
63  username_password: string[] = [];
64
65  build() {
66    Column() {
67      Button('getHttpAuthCredentials')
68        .onClick(() => {
69          try {
70            this.username_password = webview.WebDataBase.getHttpAuthCredentials(this.host, this.realm);
71            console.log('num: ' + this.username_password.length);
72          } catch (error) {
73            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
74          }
75        })
76      Web({ src: 'www.example.com', controller: this.controller })
77    }
78  }
79}
80```
81
82## saveHttpAuthCredentials
83
84static saveHttpAuthCredentials(host: string, realm: string, username: string, password: string): void
85
86Saves HTTP authentication credentials for a given host and realm. This API returns the result synchronously.
87
88**System capability**: SystemCapability.Web.Webview.Core
89
90**Parameters**
91
92| Name  | Type  | Mandatory| Description                        |
93| -------- | ------ | ---- | ---------------------------- |
94| host     | string | Yes  | Host to which the HTTP authentication credential is applied.|
95| realm    | string | Yes  | Realm to which the HTTP authentication credential is applied.  |
96| username | string | Yes  | User name.                    |
97| password | string | Yes  | Password.                      |
98
99**Error codes**
100
101For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
102
103| Error Code| Error Message                                               |
104| -------- | ------------------------------------------------------ |
105| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. |
106
107**Example**
108
109```ts
110// xxx.ets
111import { webview } from '@kit.ArkWeb';
112import { BusinessError } from '@kit.BasicServicesKit';
113
114@Entry
115@Component
116struct WebComponent {
117  controller: webview.WebviewController = new webview.WebviewController();
118  host: string = "www.spincast.org";
119  realm: string = "protected example";
120
121  build() {
122    Column() {
123      Button('saveHttpAuthCredentials')
124        .onClick(() => {
125          try {
126            webview.WebDataBase.saveHttpAuthCredentials(this.host, this.realm, "Stromgol", "Laroche");
127          } catch (error) {
128            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
129          }
130        })
131      Web({ src: 'www.example.com', controller: this.controller })
132    }
133  }
134}
135```
136
137## existHttpAuthCredentials
138
139static existHttpAuthCredentials(): boolean
140
141Checks whether any saved HTTP authentication credentials exist. This API returns the result synchronously.
142
143**System capability**: SystemCapability.Web.Webview.Core
144
145**Return value**
146
147| Type   | Description                                                        |
148| ------- | ------------------------------------------------------------ |
149| boolean | Whether any saved HTTP authentication credentials exist.<br>**true** is returned if any saved HTTP authentication credentials exist; otherwise, **false** is returned.|
150
151**Example**
152
153```ts
154// xxx.ets
155import { webview } from '@kit.ArkWeb';
156import { BusinessError } from '@kit.BasicServicesKit';
157
158@Entry
159@Component
160struct WebComponent {
161  controller: webview.WebviewController = new webview.WebviewController();
162
163  build() {
164    Column() {
165      Button('existHttpAuthCredentials')
166        .onClick(() => {
167          try {
168            let result = webview.WebDataBase.existHttpAuthCredentials();
169          } catch (error) {
170            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
171          }
172        })
173      Web({ src: 'www.example.com', controller: this.controller })
174    }
175  }
176}
177```
178
179## deleteHttpAuthCredentials
180
181static deleteHttpAuthCredentials(): void
182
183Deletes all HTTP authentication credentials saved in the cache. This API returns the result synchronously.
184
185**System capability**: SystemCapability.Web.Webview.Core
186
187**Example**
188
189```ts
190// xxx.ets
191import { webview } from '@kit.ArkWeb';
192import { BusinessError } from '@kit.BasicServicesKit';
193
194@Entry
195@Component
196struct WebComponent {
197  controller: webview.WebviewController = new webview.WebviewController();
198
199  build() {
200    Column() {
201      Button('deleteHttpAuthCredentials')
202        .onClick(() => {
203          try {
204            webview.WebDataBase.deleteHttpAuthCredentials();
205          } catch (error) {
206            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
207          }
208        })
209      Web({ src: 'www.example.com', controller: this.controller })
210    }
211  }
212}
213```
214