openharmony 鸿蒙 capi-net-http-h

2026-08-25 浏览 (1)

net_http.h

Overview

Defines the APIs of the HTTP request module.

File to include: <network/netstack/net_http.h>

Library: libnet_http.so

System capability: SystemCapability.Communication.NetStack

Since: 20

Related module: netstack

Summary

Functions

NameDescription
Http_Headers *OH_Http_CreateHeaders(void)Creates an HTTP request or response header.
void OH_Http_DestroyHeaders(Http_Headers **headers)Destroys an HTTP request or response header.
uint32_t OH_Http_SetHeaderValue(struct Http_Headers *headers, const char *name, const char *value)Sets the key-value pairs of an HTTP request or response header.
Http_HeaderValue *OH_Http_GetHeaderValue(Http_Headers *headers, const char *name)Obtains the value of an HTTP request or response header based on the specified key.
Http_HeaderEntry *OH_Http_GetHeaderEntries(Http_Headers *headers)Obtains all key-value pairs from an HTTP request or response header.
void OH_Http_DestroyHeaderEntries(Http_HeaderEntry **headerEntry)Destroys all key-value pairs obtained from OH_Http_GetHeaderEntries.
Http_Request *OH_Http_CreateRequest(const char *url)Creates an HTTP request.
int OH_Http_Request(Http_Request *request, Http_ResponseCallback callback, Http_EventsHandler handler)Initiates an HTTP request.
void OH_Http_Destroy(struct Http_Request **request)Destroys an HTTP request.

Function Description

OH_Http_CreateHeaders()

Http_Headers *OH_Http_CreateHeaders(void)

Overview

Creates an HTTP request or response header.

NOTE

You are advised to call OH_Http_DestroyHeaders to destroy the HTTP request or response header and clear resources after the HTTP request is complete.

System capability: SystemCapability.Communication.NetStack

Since: 20

Return value

TypeDescription
Http_Headers *Pointer to the HTTP request or response header.

OH_Http_DestroyHeaders()

void OH_Http_DestroyHeaders(Http_Headers **headers)

Overview

Destroys an HTTP request or response header.

System capability: SystemCapability.Communication.NetStack

Since: 20

Parameters

NameDescription
Http_Headers **headersHTTP request or response header to be destroyed. The header is generated using OH_Http_CreateHeaders.

OH_Http_SetHeaderValue()

uint32_t OH_Http_SetHeaderValue(struct Http_Headers *headers, const char *name, const char *value)

Overview

Sets the key-value pairs of an HTTP request or response header.

System capability: SystemCapability.Communication.NetStack

Since: 20

Parameters

NameDescription
struct Http_Headers *headersPointer to Http_Headers.
const char *nameKey name.
const char *valueKey value.

Return value

TypeDescription
uint32_tA status code of the uint32_t type.
0: Success.
401: Parameter error.
2300027: Insufficient memory.

OH_Http_GetHeaderValue()

Http_HeaderValue *OH_Http_GetHeaderValue(Http_Headers *headers, const char *name)

Overview

Obtains the value of an HTTP request or response header based on the specified key.

System capability: SystemCapability.Communication.NetStack

Since: 20

Parameters

NameDescription
Http_Headers *headersPointer to Http_Headers.
const char *nameKey name.

Return value

TypeDescription
Http_HeaderValue *Pointer to Http_HeaderValue.

OH_Http_GetHeaderEntries()

Http_HeaderEntry *OH_Http_GetHeaderEntries(Http_Headers *headers)

Overview

Obtains all key-value pairs from an HTTP request or response header.

System capability: SystemCapability.Communication.NetStack

Since: 20

Parameters

NameDescription
Http_Headers *headersPointer to Http_Headers.

Return value

TypeDescription
Http_HeaderEntry *Pointer to Http_HeaderEntry.

OH_Http_DestroyHeaderEntries()

void OH_Http_DestroyHeaderEntries(Http_HeaderEntry **headerEntry)

Overview

Destroys all key-value pairs obtained from OH_Http_GetHeaderEntries.

System capability: SystemCapability.Communication.NetStack

Since: 20

Parameters

NameDescription
Http_HeaderEntry **headerEntryPointer to Http_HeaderEntry, which is obtained through OH_Http_GetHeaderEntries.

OH_Http_CreateRequest()

Http_Request *OH_Http_CreateRequest(const char *url)

Overview

Creates an HTTP request.

System capability: SystemCapability.Communication.NetStack

Since: 20

Parameters

NameDescription
const char *urlRequest URL.

Return value

TypeDescription
Http_Request *Pointer to Http_Request.

OH_Http_Request()

int OH_Http_Request(Http_Request *request, Http_ResponseCallback callback, Http_EventsHandler handler)

Overview

Initiates an HTTP request.

NOTE

You are advised to call OH_Http_Destroy to interrupt the HTTP request after the HTTP request receives a response and the response is processed.

System capability: SystemCapability.Communication.NetStack

Required permission: ohos.permission.INTERNET

Since: 20

Parameters

NameDescription
Http_Request *requestPointer to Http_Request.
Http_ResponseCallback callbackHTTP response callback.
Http_EventsHandler handlerHTTP event handler.

Return value

TypeDescription
int0 if the operation is successful; an error code otherwise. For details about the error code description, see Http_ErrCode.
Http_ResponseCallback also carries errCode, indicating that the operation is successful but server communication is abnormal due to some reasons. For details, see Http_ErrCode.

OH_Http_Destroy()

void OH_Http_Destroy(struct Http_Request **request)

Overview

Stops the HTTP request.

System capability: SystemCapability.Communication.NetStack

Since: 20

Parameters

NameDescription
struct Http_Request **requestPointer to the request to be interrupted, which is a pointer to Http_Request. For details, see Http_Request.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-netconnection-netconn-connectionproperties

openharmony 鸿蒙 errorcode-net-mdns

openharmony 鸿蒙 js-apis-net-ethernet-sys

openharmony 鸿蒙 errorcode-net-http

openharmony 鸿蒙 capi-netstack-http-proxy

openharmony 鸿蒙 js-apis-system-fetch

openharmony 鸿蒙 errorcode-kernel

openharmony 鸿蒙 capi-netstack-netstack-certblob

openharmony 鸿蒙 js-apis-net-mdns

openharmony 鸿蒙 capi-netstack-websocket-closeoption

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