• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class (WebSchemeHandlerResponse)
2<!--Kit: ArkWeb-->
3<!--Subsystem: Web-->
4<!--Owner: @aohui-->
5<!--Designer: @yaomingliu-->
6<!--Tester: @ghiker-->
7<!--Adviser: @HelloCrease-->
8
9请求的响应,可以为被拦截的请求创建一个Response并填充自定义的内容返回给Web组件。
10
11> **说明:**
12>
13> - 本模块首批接口从API version 9开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
14>
15> - 本Class首批接口从API version 12开始支持。
16>
17> - 示例效果请以真机运行为准,当前DevEco Studio预览器不支持。
18
19## 导入模块
20
21```ts
22import { webview } from '@kit.ArkWeb';
23```
24
25## constructor<sup>12+</sup>
26
27constructor()
28
29Response的构造函数。
30
31**系统能力:** SystemCapability.Web.Webview.Core
32
33**示例:**
34
35```ts
36// xxx.ets
37import { webview } from '@kit.ArkWeb';
38import { BusinessError } from '@kit.BasicServicesKit';
39import { WebNetErrorList } from '@ohos.web.netErrorList';
40
41@Entry
42@Component
43struct WebComponent {
44  controller: webview.WebviewController = new webview.WebviewController();
45  schemeHandler: webview.WebSchemeHandler = new webview.WebSchemeHandler();
46
47  build() {
48    Column() {
49      Button('response').onClick(() => {
50        let response = new webview.WebSchemeHandlerResponse();
51        try {
52          response.setUrl("http://www.example.com")
53          response.setStatus(200)
54          response.setStatusText("OK")
55          response.setMimeType("text/html")
56          response.setEncoding("utf-8")
57          response.setHeaderByName("header1", "value1", false)
58          response.setNetErrorCode(WebNetErrorList.NET_OK)
59          console.info("[schemeHandler] getUrl:" + response.getUrl())
60          console.info("[schemeHandler] getStatus:" + response.getStatus())
61          console.info("[schemeHandler] getStatusText:" + response.getStatusText())
62          console.info("[schemeHandler] getMimeType:" + response.getMimeType())
63          console.info("[schemeHandler] getEncoding:" + response.getEncoding())
64          console.info("[schemeHandler] getHeaderByValue:" + response.getHeaderByName("header1"))
65          console.info("[schemeHandler] getNetErrorCode:" + response.getNetErrorCode())
66
67        } catch (error) {
68          console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
69        }
70      })
71      Web({ src: 'https://www.example.com', controller: this.controller })
72    }
73  }
74}
75
76```
77
78## setUrl<sup>12+</sup>
79
80setUrl(url: string): void
81
82给当前的Response设置重定向或因HSTS而更改后的URL,设置了url后会触发请求的跳转。
83
84**系统能力:** SystemCapability.Web.Webview.Core
85
86**参数:**
87
88| 参数名   | 类型    |  必填  | 说明                       |
89| --------| ------- | ---- | ---------------------------|
90|  url | string | 是   | 即将要跳转的URL。 |
91
92**示例:**
93
94完整示例代码参考[constructor](#constructor12)。
95
96**错误码:**
97
98以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
99
100| 错误码ID | 错误信息                  |
101| -------- | ----------------------- |
102|  401 | Parameter error. Possible causes: 1. Incorrect parameter types.    |
103
104## setNetErrorCode<sup>12+</sup>
105
106setNetErrorCode(code: WebNetErrorList): void
107
108给当前的Response设置网络错误码。
109
110**系统能力:** SystemCapability.Web.Webview.Core
111
112**参数:**
113
114| 参数名   | 类型    |  必填  | 说明                       |
115| --------| ------- | ---- | ---------------------------|
116|  code | [WebNetErrorList](arkts-apis-netErrorList.md#webneterrorlist) | 是   | 网络错误码。 |
117
118**错误码:**
119
120以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
121
122| 错误码ID | 错误信息                  |
123| -------- | ----------------------- |
124|  401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.    |
125
126**示例:**
127
128完整示例代码参考[constructor](#constructor12)。
129
130## setStatus<sup>12+</sup>
131
132setStatus(code: number): void
133
134给当前的Response设置HTTP状态码。
135
136**系统能力:** SystemCapability.Web.Webview.Core
137
138**参数:**
139
140| 参数名   | 类型    |  必填  | 说明                       |
141| --------| ------- | ---- | ---------------------------|
142|  code | number | 是   | Http状态码。 |
143
144**错误码:**
145
146以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
147
148| 错误码ID | 错误信息                  |
149| -------- | ----------------------- |
150|  401 | Parameter error. Possible causes: 1. Incorrect parameter types. |
151
152**示例:**
153
154完整示例代码参考[constructor](#constructor12)。
155
156## setStatusText<sup>12+</sup>
157
158setStatusText(text: string): void
159
160给当前的Response设置状态文本。
161
162**系统能力:** SystemCapability.Web.Webview.Core
163
164**参数:**
165
166| 参数名   | 类型    |  必填  | 说明                       |
167| --------| ------- | ---- | ---------------------------|
168|  text | string | 是   | 状态文本。 |
169
170**错误码:**
171
172以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
173
174| 错误码ID | 错误信息                  |
175| -------- | ----------------------- |
176|  401 | Parameter error. Possible causes: 1. Incorrect parameter types.    |
177
178**示例:**
179
180完整示例代码参考[constructor](#constructor12)。
181
182## setMimeType<sup>12+</sup>
183
184setMimeType(type: string): void
185
186给当前的Response设置媒体类型。
187
188**系统能力:** SystemCapability.Web.Webview.Core
189
190**参数:**
191
192| 参数名   | 类型    |  必填  | 说明                       |
193| --------| ------- | ---- | ---------------------------|
194|  type | string | 是   | 媒体类型。 |
195
196**错误码:**
197
198以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
199
200| 错误码ID | 错误信息                  |
201| -------- | ----------------------- |
202|  401 | Parameter error. Possible causes: 1. Incorrect parameter types.    |
203
204**示例:**
205
206完整示例代码参考[constructor](#constructor12)。
207
208## setEncoding<sup>12+</sup>
209
210setEncoding(encoding: string): void
211
212给当前的Response设置字符集。
213
214**系统能力:** SystemCapability.Web.Webview.Core
215
216**参数:**
217
218| 参数名   | 类型    |  必填  | 说明                       |
219| --------| ------- | ---- | ---------------------------|
220|  encoding | string | 是   | 字符集。 |
221
222**错误码:**
223
224以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
225
226| 错误码ID | 错误信息                  |
227| -------- | ----------------------- |
228|  401 | Parameter error. Possible causes: 1. Incorrect parameter types.    |
229
230**示例:**
231
232完整示例代码参考[constructor](#constructor12)。
233
234## setHeaderByName<sup>12+</sup>
235
236setHeaderByName(name: string, value: string, overwrite: boolean): void
237
238给当前的Response设置头信息。
239
240**系统能力:** SystemCapability.Web.Webview.Core
241
242**参数:**
243
244| 参数名   | 类型    |  必填  | 说明                       |
245| --------| ------- | ---- | ---------------------------|
246|  name | string | 是   | 头部(header)的名称。 |
247|  value | string | 是   | 头部(header)的值。 |
248|  overwrite | boolean | 是   | 如果为true,将覆盖现有的头部,否则不覆盖。 |
249
250**错误码:**
251
252以下错误码的详细介绍请参见[webview错误码](errorcode-webview.md)。
253
254| 错误码ID | 错误信息                  |
255| -------- | ----------------------- |
256|  401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.    |
257
258**示例:**
259
260完整示例代码参考[constructor](#constructor12)。
261
262## getUrl<sup>12+</sup>
263
264getUrl(): string
265
266获取重定向或由于HSTS而更改后的URL。
267风险提示:如果想获取url来做JavascriptProxy通信接口认证,请使用[getLastJavascriptProxyCallingFrameUrl<sup>12+</sup>](./arkts-apis-webview-WebviewController.md#getlastjavascriptproxycallingframeurl12)
268
269**系统能力:** SystemCapability.Web.Webview.Core
270
271**返回值:**
272
273| 类型    | 说明                                     |
274| ------- | --------------------------------------- |
275| string | 获取经过重定向或由于HSTS而更改后的URL。|
276
277**示例:**
278
279完整示例代码参考[constructor](#constructor12)。
280
281## getNetErrorCode<sup>12+</sup>
282
283getNetErrorCode(): WebNetErrorList
284
285获取Response的网络错误码。
286
287**系统能力:** SystemCapability.Web.Webview.Core
288
289**返回值:**
290
291| 类型    | 说明                                     |
292| ------- | --------------------------------------- |
293| [WebNetErrorList](arkts-apis-netErrorList.md#webneterrorlist) | 获取Response的网络错误码。|
294
295**示例:**
296
297完整示例代码参考[constructor](#constructor12)。
298
299## getStatus<sup>12+</sup>
300
301getStatus(): number
302
303获取Response的Http状态码。
304
305**系统能力:** SystemCapability.Web.Webview.Core
306
307**返回值:**
308
309| 类型    | 说明                                     |
310| ------- | --------------------------------------- |
311| number | 获取Response的Http状态码。|
312
313**示例:**
314
315完整示例代码参考[constructor](#constructor12)。
316
317## getStatusText<sup>12+</sup>
318
319getStatusText(): string
320
321获取Response的状态文本。
322
323**系统能力:** SystemCapability.Web.Webview.Core
324
325**返回值:**
326
327| 类型    | 说明                                     |
328| ------- | --------------------------------------- |
329| string | 状态文本。|
330
331**示例:**
332
333完整示例代码参考[constructor](#constructor12)。
334
335## getMimeType<sup>12+</sup>
336
337getMimeType(): string
338
339获取Response的媒体类型。
340
341**系统能力:** SystemCapability.Web.Webview.Core
342
343**返回值:**
344
345| 类型    | 说明                                     |
346| ------- | --------------------------------------- |
347| string | 媒体类型。|
348
349**示例:**
350
351完整示例代码参考[constructor](#constructor12)。
352
353## getEncoding<sup>12+</sup>
354
355getEncoding(): string
356
357获取Response的字符集。
358
359**系统能力:** SystemCapability.Web.Webview.Core
360
361**返回值:**
362
363| 类型    | 说明                                     |
364| ------- | --------------------------------------- |
365| string | 字符集。|
366
367**示例:**
368
369完整示例代码参考[constructor](#constructor12)。
370
371## getHeaderByName<sup>12+</sup>
372
373getHeaderByName(name: string): string
374
375获取Response的字符集。
376
377**系统能力:** SystemCapability.Web.Webview.Core
378
379**参数:**
380
381| 参数名  | 类型             | 必填 | 说明                  |
382| ------- | ---------------- | ---- | -------------------- |
383| name     | string | 是   | 头部(header)的名称。      |
384
385
386**返回值:**
387
388| 类型    | 说明                                     |
389| ------- | --------------------------------------- |
390| string | 头部(header)的值。|
391
392**示例:**
393
394完整示例代码参考[constructor](#constructor12)。