openharmony 鸿蒙 arkts-apis-webview-WebSchemeHandlerResponse

2026-08-25 浏览 (1)

Class (WebSchemeHandlerResponse)

Represents a request response. You can create a response for an intercepted request, fill in custom content, and return the response to the Web component.

NOTE

  • 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.

  • The initial APIs of this class are supported since API version 12.

  • The sample effect is subject to the actual device.

Modules to Import

import { webview } from '@kit.ArkWeb';

constructor12+

constructor()

Constructs a Response object.

System capability: SystemCapability.Web.Webview.Core

Example

// xxx.ets
import { webview, WebNetErrorList } from '@kit.ArkWeb';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();
  schemeHandler: webview.WebSchemeHandler = new webview.WebSchemeHandler();

  build() {
    Column() {
      Button('response').onClick(() => {
        let response = new webview.WebSchemeHandlerResponse();
        try {
          response.setUrl("http://www.example.com")
          response.setStatus(200)
          response.setStatusText("OK")
          response.setMimeType("text/html")
          response.setEncoding("utf-8")
          response.setHeaderByName("header1", "value1", false)
          response.setNetErrorCode(WebNetErrorList.NET_OK)
          console.info("[schemeHandler] getUrl:" + response.getUrl())
          console.info("[schemeHandler] getStatus:" + response.getStatus())
          console.info("[schemeHandler] getStatusText:" + response.getStatusText())
          console.info("[schemeHandler] getMimeType:" + response.getMimeType())
          console.info("[schemeHandler] getEncoding:" + response.getEncoding())
          console.info("[schemeHandler] getHeaderByValue:" + response.getHeaderByName("header1"))
          console.info("[schemeHandler] getNetErrorCode:" + response.getNetErrorCode())

        } catch (error) {
          console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
        }
      })
      Web({ src: 'https://www.example.com', controller: this.controller })
    }
  }
}

setUrl12+

setUrl(url: string): void

Sets 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.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
urlstringYesURL to be redirected to.

Example

For details about the sample code, see constructor.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Incorrect parameter types.

setNetErrorCode12+

setNetErrorCode(code: WebNetErrorList): void

Sets the network error code for this response.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
codeWebNetErrorListYesNetwork error code.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

For details about the sample code, see constructor.

setStatus12+

setStatus(code: number): void

Sets the HTTP status code for this response.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
codenumberYesHTTP status code.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Incorrect parameter types.

Example

For details about the sample code, see constructor.

setStatusText12+

setStatusText(text: string): void

Sets the status text for this response.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
textstringYesStatus text.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Incorrect parameter types.

Example

For details about the sample code, see constructor.

setMimeType12+

setMimeType(type: string): void

Sets the MIME type for this response.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
typestringYesMIME type.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Incorrect parameter types.

Example

For details about the sample code, see constructor.

setEncoding12+

setEncoding(encoding: string): void

Sets the character set for this response.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
encodingstringYesCharacter set.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Incorrect parameter types.

Example

For details about the sample code, see constructor.

setHeaderByName12+

setHeaderByName(name: string, value: string, overwrite: boolean): void

Sets the header information for this response.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
namestringYesName of the header.
valuestringYesValue of the header.
overwritebooleanYesWhether to override the existing header. The value true means to override the existing header, and false means the opposite.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

For details about the sample code, see constructor.

getUrl12+

getUrl(): string

Obtains the redirection URL or the URL changed due to HSTS.

NOTE: To obtain a URL for the JavaScriptProxy communication API authentication, use getLastJavascriptProxyCallingFrameUrl12+.

System capability: SystemCapability.Web.Webview.Core

Return value

TypeDescription
stringRedirection URL or the URL changed due to HSTS.

Example

For details about the sample code, see constructor.

getNetErrorCode12+

getNetErrorCode(): WebNetErrorList

Obtains the network error code of the response.

System capability: SystemCapability.Web.Webview.Core

Return value

TypeDescription
WebNetErrorListObtains the network error code of the response.

Example

For details about the sample code, see constructor.

getStatus12+

getStatus(): number

Obtains the HTTP status code of the response.

System capability: SystemCapability.Web.Webview.Core

Return value

TypeDescription
numberHTTP status code of the response.

Example

For details about the sample code, see constructor.

getStatusText12+

getStatusText(): string

Obtains the status text of this response.

System capability: SystemCapability.Web.Webview.Core

Return value

TypeDescription
stringStatus text.

Example

For details about the sample code, see constructor.

getMimeType12+

getMimeType(): string

Obtains the MIME type of this response.

System capability: SystemCapability.Web.Webview.Core

Return value

TypeDescription
stringMIME type.

Example

For details about the sample code, see constructor.

getEncoding12+

getEncoding(): string

Obtains the character set of this response.

System capability: SystemCapability.Web.Webview.Core

Return value

TypeDescription
stringCharacter set.

Example

For details about the sample code, see constructor.

getHeaderByName12+

getHeaderByName(name: string): string

Obtains the value of a response header field by name.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
namestringYesName of the header.

Return value

TypeDescription
stringValue of the header.

Example

For details about the sample code, see constructor.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 arkts-apis-webview-WebviewController

openharmony 鸿蒙 arkts-apis-webview-t

openharmony 鸿蒙 arkts-apis-webview

openharmony 鸿蒙 capi-arkweb-type-h

openharmony 鸿蒙 arkts-basic-components-web-FullScreenExitHandler

openharmony 鸿蒙 arkts-apis-webview-WebDownloadManager

openharmony 鸿蒙 arkts-basic-components-web-VerifyPinHandler

openharmony 鸿蒙 capi-web-arkweb-webmessage8h

openharmony 鸿蒙 arkts-apis-webview-NativeMediaPlayerHandler

openharmony 鸿蒙 capi-web-arkweb-proxymethodwithresult

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/WRzHHasf