openharmony 鸿蒙 arkts-apis-webview-WebHttpBodyStream

2026-08-25 浏览 (1)

Class (WebHttpBodyStream)

Represents the body of the data being sent in POST and PUT requests. It accepts data of the BYTES, FILE, BLOB, and CHUNKED types. Note that other APIs in this class can be called only after initialize is called successfully.

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';

initialize12+

initialize(): Promise<void>

Initializes this WebHttpBodyStream instance.

System capability: SystemCapability.Web.Webview.Core

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

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

Error CodeError Message
17100022Failed to initialize the HTTP body stream.

Example

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

@Entry
@Component
struct WebComponent {
  controller: webview.WebviewController = new webview.WebviewController();
  schemeHandler: webview.WebSchemeHandler = new webview.WebSchemeHandler();
  htmlData: string = "<html><body bgcolor=\"white\">Source:<pre>source</pre></body></html>";

  build() {
    Column() {
      Button('postUrl')
        .onClick(() => {
          try {
            let postData = buffer.from(this.htmlData);
            this.controller.postUrl('https://www.example.com', postData.buffer);
          } catch (error) {
            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          }
        })
      Web({ src: 'https://www.example.com', controller: this.controller })
        .onControllerAttached(() => {
          try {
            this.schemeHandler.onRequestStart((request: webview.WebSchemeHandlerRequest, resourceHandler: webview.WebResourceHandler) => {
              console.info("[schemeHandler] onRequestStart");
              try {
                let stream = request.getHttpBodyStream();
                if (stream) {
                  stream.initialize().then(() => {
                    if (!stream) {
                      return;
                    }
                    console.info("[schemeHandler] onRequestStart postDataStream size:" + stream.getSize());
                    console.info("[schemeHandler] onRequestStart postDataStream position:" + stream.getPosition());
                    console.info("[schemeHandler] onRequestStart postDataStream isChunked:" + stream.isChunked());
                    console.info("[schemeHandler] onRequestStart postDataStream isEof:" + stream.isEof());
                    console.info("[schemeHandler] onRequestStart postDataStream isInMemory:" + stream.isInMemory());
                    stream.read(stream.getSize()).then((buffer) => {
                      if (!stream) {
                        return;
                      }
                      console.info("[schemeHandler] onRequestStart postDataStream readlength:" + buffer.byteLength);
                      console.info("[schemeHandler] onRequestStart postDataStream isEof:" + stream.isEof());
                      console.info("[schemeHandler] onRequestStart postDataStream position:" + stream.getPosition());
                    }).catch((error: BusinessError) => {
                      console.error(`ErrorCode: ${error.code},  Message: ${error.message}`);
                    })
                  }).catch((error: BusinessError) => {
                    console.error(`ErrorCode: ${error.code},  Message: ${error.message}`);
                  })
                } else {
                  console.info("[schemeHandler] onRequestStart has no http body stream");
                }
              } catch (error) {
                console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
              }

              return false;
            })

            this.schemeHandler.onRequestStop((request: webview.WebSchemeHandlerRequest) => {
              console.info("[schemeHandler] onRequestStop");
            });

            this.controller.setWebSchemeHandler('https', this.schemeHandler);
          } catch (error) {
            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
          }
        })
        .javaScriptAccess(true)
        .domStorageAccess(true)
    }
  }
}

read12+

read(size: number): Promise<ArrayBuffer>

Reads data from this WebHttpBodyStream instance.

System capability: SystemCapability.Web.Webview.Core

Parameters

NameTypeMandatoryDescription
sizenumberYesNumber of bytes obtained from the WebHttpBodyStream instance.

Return value

TypeDescription
Promise<ArrayBuffer>Promise used to return the result.

Error codes

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

Error CodeError Message
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed.

Example

For the complete sample code, see initialize.

getSize12+

getSize(): number

Obtains the size of data in this WebHttpBodyStream instance. This API always returns zero when chunked transfer is used.

System capability: SystemCapability.Web.Webview.Core

Return value

TypeDescription
numberSize of data in the current WebHttpBodyStream instance.

Example

For the complete sample code, see initialize.

getPosition12+

getPosition(): number

Reads the current read position in this WebHttpBodyStream instance.

System capability: SystemCapability.Web.Webview.Core

Return value

TypeDescription
numberCurrent read position in the WebHttpBodyStream instance.

Example

For the complete sample code, see initialize.

isChunked12+

isChunked(): boolean

Checks whether this WebHttpBodyStream instance is transmitted by chunk.

System capability: SystemCapability.Web.Webview.Core

Return value

TypeDescription
booleanWhether the WebHttpBodyStream instance is transmitted by chunk. The value true indicates that the WebHttpBodyStream instance is transmitted by chunk, and false indicates the opposite.

Example

For the complete sample code, see initialize.

isEof12+

isEof(): boolean

Checks whether all data in this WebHttpBodyStream instance has been read.

System capability: SystemCapability.Web.Webview.Core

Return value

TypeDescription
booleanWhether all data in the WebHttpBodyStream instance has been read.
This API returns true if all data in the WebHttpBodyStream instance is read. It returns false before the first read attempt is made for the WebHttpBodyStream instance that uses chunked transfer.

Example

For the complete sample code, see initialize.

isInMemory12+

isInMemory(): boolean

Checks whether the uploaded data in this WebHttpBodyStream instance is in memory.

System capability: SystemCapability.Web.Webview.Core

Return value

TypeDescription
booleanWhether the uploaded data in the WebHttpBodyStream instance is stored in memory.
This API returns true if all the upload data in the WebHttpBodyStream instance is in memory and all read requests will be completed synchronously. false is returned if the data is chunked.

Example

For the complete sample code, see initialize.

你可能感兴趣的鸿蒙文章

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/Ppsamy9T