• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class (WebSchemeHandlerResponse)
2
3Represents a request response. You can create a response for an intercepted request, fill in custom content, and return the response to the **Web** component.
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 12.
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## constructor<sup>12+</sup>
20
21constructor()
22
23Constructs a **Response** object.
24
25**System capability**: SystemCapability.Web.Webview.Core
26
27**Example**
28
29```ts
30// xxx.ets
31import { webview } from '@kit.ArkWeb';
32import { BusinessError } from '@kit.BasicServicesKit';
33import { WebNetErrorList } from '@ohos.web.netErrorList';
34
35@Entry
36@Component
37struct WebComponent {
38  controller: webview.WebviewController = new webview.WebviewController();
39  schemeHandler: webview.WebSchemeHandler = new webview.WebSchemeHandler();
40
41  build() {
42    Column() {
43      Button('response').onClick(() => {
44        let response = new webview.WebSchemeHandlerResponse();
45        try {
46          response.setUrl("http://www.example.com")
47          response.setStatus(200)
48          response.setStatusText("OK")
49          response.setMimeType("text/html")
50          response.setEncoding("utf-8")
51          response.setHeaderByName("header1", "value1", false)
52          response.setNetErrorCode(WebNetErrorList.NET_OK)
53          console.log("[schemeHandler] getUrl:" + response.getUrl())
54          console.log("[schemeHandler] getStatus:" + response.getStatus())
55          console.log("[schemeHandler] getStatusText:" + response.getStatusText())
56          console.log("[schemeHandler] getMimeType:" + response.getMimeType())
57          console.log("[schemeHandler] getEncoding:" + response.getEncoding())
58          console.log("[schemeHandler] getHeaderByValue:" + response.getHeaderByName("header1"))
59          console.log("[schemeHandler] getNetErrorCode:" + response.getNetErrorCode())
60
61        } catch (error) {
62          console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
63        }
64      })
65      Web({ src: 'https://www.example.com', controller: this.controller })
66    }
67  }
68}
69
70```
71
72## setUrl<sup>12+</sup>
73
74setUrl(url: string): void
75
76Sets the redirection URL or the URL changed due to HSTS for this response. After the URL is set, a redirection to the new URL is triggered.
77
78**System capability**: SystemCapability.Web.Webview.Core
79
80**Parameters**
81
82| Name  | Type   |  Mandatory | Description                      |
83| --------| ------- | ---- | ---------------------------|
84|  url | string | Yes  | URL to be redirected to.|
85
86**Example**
87
88For details about the sample code, see [constructor](#constructor12).
89
90**Error codes**
91
92For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
93
94| ID| Error Message                 |
95| -------- | ----------------------- |
96|  401 | Parameter error. Possible causes: 1. Incorrect parameter types.    |
97
98## setNetErrorCode<sup>12+</sup>
99
100setNetErrorCode(code: WebNetErrorList): void
101
102Sets the network error code for this response.
103
104**System capability**: SystemCapability.Web.Webview.Core
105
106**Parameters**
107
108| Name  | Type   |  Mandatory | Description                      |
109| --------| ------- | ---- | ---------------------------|
110|  code | [WebNetErrorList](arkts-apis-netErrorList.md#webneterrorlist) | Yes  | Network error code.|
111
112**Error codes**
113
114For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
115
116| ID| Error Message                 |
117| -------- | ----------------------- |
118|  401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.    |
119
120**Example**
121
122For details about the sample code, see [constructor](#constructor12).
123
124## setStatus<sup>12+</sup>
125
126setStatus(code: number): void
127
128Sets the HTTP status code for this response.
129
130**System capability**: SystemCapability.Web.Webview.Core
131
132**Parameters**
133
134| Name  | Type   |  Mandatory | Description                      |
135| --------| ------- | ---- | ---------------------------|
136|  code | number | Yes  | HTTP status code.|
137
138**Error codes**
139
140For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
141
142| ID| Error Message                 |
143| -------- | ----------------------- |
144|  401 | Parameter error. Possible causes: 1. Incorrect parameter types. |
145
146**Example**
147
148For details about the sample code, see [constructor](#constructor12).
149
150## setStatusText<sup>12+</sup>
151
152setStatusText(text: string): void
153
154Sets the status text for this response.
155
156**System capability**: SystemCapability.Web.Webview.Core
157
158**Parameters**
159
160| Name  | Type   |  Mandatory | Description                      |
161| --------| ------- | ---- | ---------------------------|
162|  text | string | Yes  | Status text.|
163
164**Error codes**
165
166For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
167
168| ID| Error Message                 |
169| -------- | ----------------------- |
170|  401 | Parameter error. Possible causes: 1. Incorrect parameter types.    |
171
172**Example**
173
174For details about the sample code, see [constructor](#constructor12).
175
176## setMimeType<sup>12+</sup>
177
178setMimeType(type: string): void
179
180Sets the MIME type for this response.
181
182**System capability**: SystemCapability.Web.Webview.Core
183
184**Parameters**
185
186| Name  | Type   |  Mandatory | Description                      |
187| --------| ------- | ---- | ---------------------------|
188|  type | string | Yes  | MIME type.|
189
190**Error codes**
191
192For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
193
194| ID| Error Message                 |
195| -------- | ----------------------- |
196|  401 | Parameter error. Possible causes: 1. Incorrect parameter types.    |
197
198**Example**
199
200For details about the sample code, see [constructor](#constructor12).
201
202## setEncoding<sup>12+</sup>
203
204setEncoding(encoding: string): void
205
206Sets the character set for this response.
207
208**System capability**: SystemCapability.Web.Webview.Core
209
210**Parameters**
211
212| Name  | Type   |  Mandatory | Description                      |
213| --------| ------- | ---- | ---------------------------|
214|  encoding | string | Yes  | Character set.|
215
216**Error codes**
217
218For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
219
220| ID| Error Message                 |
221| -------- | ----------------------- |
222|  401 | Parameter error. Possible causes: 1. Incorrect parameter types.    |
223
224**Example**
225
226For details about the sample code, see [constructor](#constructor12).
227
228## setHeaderByName<sup>12+</sup>
229
230setHeaderByName(name: string, value: string, overwrite: boolean): void
231
232Sets the header information for this response.
233
234**System capability**: SystemCapability.Web.Webview.Core
235
236**Parameters**
237
238| Name  | Type   |  Mandatory | Description                      |
239| --------| ------- | ---- | ---------------------------|
240|  name | string | Yes  | Name of the header.|
241|  value | string | Yes  | Value of the header.|
242|  overwrite | boolean | Yes  | Whether to override the existing header. The value **true** means to override the existing header, and **false** means the opposite.|
243
244**Error codes**
245
246For details about the error codes, see [Webview Error Codes](errorcode-webview.md).
247
248| ID| Error Message                 |
249| -------- | ----------------------- |
250|  401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.    |
251
252**Example**
253
254For details about the sample code, see [constructor](#constructor12).
255
256## getUrl<sup>12+</sup>
257
258getUrl(): string
259
260Obtains the redirection URL or the URL changed due to HSTS.
261NOTE: To obtain a URL for the JavaScriptProxy communication API authentication, use [getLastJavascriptProxyCallingFrameUrl<sup>12+</sup>](./arkts-apis-webview-WebviewController.md#getlastjavascriptproxycallingframeurl12).
262
263**System capability**: SystemCapability.Web.Webview.Core
264
265**Return value**
266
267| Type   | Description                                    |
268| ------- | --------------------------------------- |
269| string | Redirection URL or the URL changed due to HSTS.|
270
271**Example**
272
273For details about the sample code, see [constructor](#constructor12).
274
275## getNetErrorCode<sup>12+</sup>
276
277getNetErrorCode(): WebNetErrorList
278
279Obtains the network error code of the response.
280
281**System capability**: SystemCapability.Web.Webview.Core
282
283**Return value**
284
285| Type   | Description                                    |
286| ------- | --------------------------------------- |
287| [WebNetErrorList](arkts-apis-netErrorList.md#webneterrorlist) | Obtains the network error code of the response.|
288
289**Example**
290
291For details about the sample code, see [constructor](#constructor12).
292
293## getStatus<sup>12+</sup>
294
295getStatus(): number
296
297Obtains the HTTP status code of the response.
298
299**System capability**: SystemCapability.Web.Webview.Core
300
301**Return value**
302
303| Type   | Description                                    |
304| ------- | --------------------------------------- |
305| number | HTTP status code of the response.|
306
307**Example**
308
309For details about the sample code, see [constructor](#constructor12).
310
311## getStatusText<sup>12+</sup>
312
313getStatusText(): string
314
315Obtains the status text of this response.
316
317**System capability**: SystemCapability.Web.Webview.Core
318
319**Return value**
320
321| Type   | Description                                    |
322| ------- | --------------------------------------- |
323| string | Status text.|
324
325**Example**
326
327For details about the sample code, see [constructor](#constructor12).
328
329## getMimeType<sup>12+</sup>
330
331getMimeType(): string
332
333Obtains the MIME type of this response.
334
335**System capability**: SystemCapability.Web.Webview.Core
336
337**Return value**
338
339| Type   | Description                                    |
340| ------- | --------------------------------------- |
341| string | MIME type.|
342
343**Example**
344
345For details about the sample code, see [constructor](#constructor12).
346
347## getEncoding<sup>12+</sup>
348
349getEncoding(): string
350
351Obtains the character set of this response.
352
353**System capability**: SystemCapability.Web.Webview.Core
354
355**Return value**
356
357| Type   | Description                                    |
358| ------- | --------------------------------------- |
359| string | Character set.|
360
361**Example**
362
363For details about the sample code, see [constructor](#constructor12).
364
365## getHeaderByName<sup>12+</sup>
366
367getHeaderByName(name: string): string
368
369Obtains the character set of this response.
370
371**System capability**: SystemCapability.Web.Webview.Core
372
373**Parameters**
374
375| Name | Type            | Mandatory| Description                 |
376| ------- | ---------------- | ---- | -------------------- |
377| name     | string | Yes  | Name of the header.     |
378
379
380**Return value**
381
382| Type   | Description                                    |
383| ------- | --------------------------------------- |
384| string | Value of the header.|
385
386**Example**
387
388For details about the sample code, see [constructor](#constructor12).
389