openharmony 鸿蒙 capi-net-connection-h

2026-08-25 浏览 (1)

net_connection.h

Overview

Provides the C APIs of the network connection module for network management.

File to include: <network/netmanager/net_connection.h>

Library: libnet_connection.so

System capability: SystemCapability.Communication.NetManager.Core

Since: 11

Related module: NetConnection

Summary

Functions

NameDescription
int32_t OH_NetConn_HasDefaultNet(int32_t *hasDefaultNet)Checks whether a default activated data network is available.
int32_t OH_NetConn_GetDefaultNet(NetConn_NetHandle *netHandle)Obtains the default activated data network.
int32_t OH_NetConn_IsDefaultNetMetered(int32_t *isMetered)Checks whether data traffic usage on the current network is metered.
int32_t OH_NetConn_GetConnectionProperties(NetConn_NetHandle *netHandle, NetConn_ConnectionProperties *prop)Obtains the link information of a data network.
int32_t OH_NetConn_GetNetCapabilities(NetConn_NetHandle *netHandle, NetConn_NetCapabilities *netCapabilities)Obtains the capabilities of a data network.
int32_t OH_NetConn_GetDefaultHttpProxy(NetConn_HttpProxy *httpProxy)Obtains the default network proxy.
int32_t OH_NetConn_GetAddrInfo(char *host, char *serv, struct addrinfo *hint, struct addrinfo **res, int32_t netId)Obtains the DNS result based on the specified netId.
int32_t OH_NetConn_FreeDnsResult(struct addrinfo *res)Releases the DNS query result.
int32_t OH_NetConn_GetAllNets(NetConn_NetHandleList *netHandleList)Obtains all activated data networks.
int32_t OHOS_NetConn_RegisterDnsResolver(OH_NetConn_CustomDnsResolver resolver)Registers a custom DNS resolver.
int32_t OHOS_NetConn_UnregisterDnsResolver(void)Unregisters a custom DNS resolver.
int32_t OH_NetConn_RegisterDnsResolver(OH_NetConn_CustomDnsResolver resolver)Registers a custom DNS resolver.
int32_t OH_NetConn_UnregisterDnsResolver(void)Unregisters a custom DNS resolver.
int32_t OH_NetConn_BindSocket(int32_t socketFd, NetConn_NetHandle *netHandle)Binds a socket to the specified network.
int32_t OH_NetConn_SetAppHttpProxy(NetConn_HttpProxy *httpProxy)Sets an HTTP proxy for the current application.
int32_t OH_NetConn_RegisterAppHttpProxyCallback(OH_NetConn_AppHttpProxyChange appHttpProxyChange, uint32_t *callbackId)Registers a callback for application-level HTTP proxy changes.
void OH_NetConn_UnregisterAppHttpProxyCallback(uint32_t callbackId)Unregisters the callback for HTTP proxy changes of the application.
int32_t OH_NetConn_RegisterNetConnCallback(NetConn_NetSpecifier *specifier, NetConn_NetConnCallback *netConnCallback,uint32_t timeout, uint32_t *callbackId)Registers a callback for network status changes.
int32_t OH_NetConn_RegisterDefaultNetConnCallback(NetConn_NetConnCallback *netConnCallback, uint32_t *callbackId)Registers a callback for status changes of the default network.
int32_t OH_NetConn_UnregisterNetConnCallback(uint32_t callBackId)Unregisters the callback for network status changes.
NetConn_ErrorCode OH_NetConn_SetPacUrl(const char *pacUrl)Sets the URL of the system-level proxy auto-config (PAC) script.
NetConn_ErrorCode OH_NetConn_GetPacUrl(char *pacUrl)Obtains the URL of the system-level PAC script.
int32_t OH_NetConn_QueryProbeResult(char *destination, int32_t duration, NetConn_ProbeResultInfo *probeResultInfo)Queries network probe results.
int32_t OH_NetConn_QueryTraceRoute(char *destination, NetConn_TraceRouteOption *option,NetConn_TraceRouteInfo *traceRouteInfo)Queries network trace route information.

Function Description

OH_NetConn_HasDefaultNet()

int32_t OH_NetConn_HasDefaultNet(int32_t *hasDefaultNet)

Description

Checks whether a default activated data network is available.

System capability: SystemCapability.Communication.NetManager.Core

Required permissions: ohos.permission.GET_NETWORK_INFO

Since: 11

Parameters

NameDescription
int32_t *hasDefaultNetWhether there is a default network.

Returns

TypeDescription
int32_t0: Success. 201: Missing permissions.
401: Parameter error. 2100002: Service connection failure.
2100003: Internal error.

OH_NetConn_GetDefaultNet()

int32_t OH_NetConn_GetDefaultNet(NetConn_NetHandle *netHandle)

Description

Obtains the default activated data network.

System capability: SystemCapability.Communication.NetManager.Core

Required permissions: ohos.permission.GET_NETWORK_INFO

Since: 11

Parameters

NameDescription
NetConn_NetHandle *netHandleNetwork ID.

Returns

TypeDescription
int32_t0: Success. 201: Missing permissions.
401: Parameter error. 2100002: Service connection failure.
2100003: Internal error.

OH_NetConn_IsDefaultNetMetered()

int32_t OH_NetConn_IsDefaultNetMetered(int32_t *isMetered)

Description

Checks whether metering is enabled for the default data network.

System capability: SystemCapability.Communication.NetManager.Core

Required permissions: ohos.permission.GET_NETWORK_INFO

Since: 11

Parameters

NameDescription
int32_t *isMeteredWhether metering is enabled.

Returns

TypeDescription
int32_t0: Success. 201: Missing permissions.
401: Parameter error. 2100002: Service connection failure.
2100003: Internal error.

OH_NetConn_GetConnectionProperties()

int32_t OH_NetConn_GetConnectionProperties(NetConn_NetHandle *netHandle, NetConn_ConnectionProperties *prop)

Description

Obtains the link information of a data network.

System capability: SystemCapability.Communication.NetManager.Core

Required permissions: ohos.permission.GET_NETWORK_INFO

Since: 11

Parameters

NameDescription
NetConn_NetHandle *netHandleNetwork ID.
NetConn_ConnectionProperties *propLink information.

Returns

TypeDescription
int32_t0: Success. 201: Missing permissions.
401: Parameter error. 2100002: Service connection failure.
2100003: Internal error.

OH_NetConn_GetNetCapabilities()

int32_t OH_NetConn_GetNetCapabilities(NetConn_NetHandle *netHandle, NetConn_NetCapabilities *netCapabilities)

Description

Obtains the capabilities of a data network.

System capability: SystemCapability.Communication.NetManager.Core

Required permissions: ohos.permission.GET_NETWORK_INFO

Since: 11

Parameters

NameDescription
NetConn_NetHandle *netHandleNetwork ID.
NetConn_NetCapabilities *netCapabilitiesCapability set.

Returns

TypeDescription
int32_t0: Success. 201: Missing permissions.
401: Parameter error. 2100002: Service connection failure.
2100003: Internal error.

OH_NetConn_GetDefaultHttpProxy()

int32_t OH_NetConn_GetDefaultHttpProxy(NetConn_HttpProxy *httpProxy)

Description

Obtains the default network proxy.

System capability: SystemCapability.Communication.NetManager.Core

Since: 11

Parameters

NameDescription
NetConn_HttpProxy *httpProxyProxy configuration.

Returns

TypeDescription
int32_t0: Success. 201: Missing permissions.
401: Parameter error. 2100002: Service connection failure.
2100003: Internal error.

OH_NetConn_GetAddrInfo()

int32_t OH_NetConn_GetAddrInfo(char *host, char *serv, struct addrinfo *hint, struct addrinfo **res, int32_t netId)

Description

Obtains the DNS result based on the specified netId.

System capability: SystemCapability.Communication.NetManager.Core

Required permission: ohos.permission.INTERNET

Since: 11

Parameters

NameDescription
char *hostHost name.
char *servService name.
struct addrinfo *hintPointer to the addrinfo structure.
struct addrinfo **resDNS query result, which is in the format of linked lists.
int32_t netIdIf netId is set to 0, the default netid is used for query.

Returns

TypeDescription
int32_t0: Success. 201: Missing permissions.
401: Parameter error. 2100002: Service connection failure.
2100003: Internal error.

OH_NetConn_FreeDnsResult()

int32_t OH_NetConn_FreeDnsResult(struct addrinfo *res)

Description

Releases the DNS query result.

System capability: SystemCapability.Communication.NetManager.Core

Required permission: ohos.permission.INTERNET

Since: 11

Parameters

NameDescription
struct addrinfo *resHeader of the DNS query result, which is in the format of linked lists.

Returns

TypeDescription
int32_t0: Success. 201: Missing permissions.
401: Parameter error. 2100002: Service connection failure.
2100003: Internal error.

OH_NetConn_GetAllNets()

int32_t OH_NetConn_GetAllNets(NetConn_NetHandleList *netHandleList)

Description

Obtains all activated data networks.

System capability: SystemCapability.Communication.NetManager.Core

Required permissions: ohos.permission.GET_NETWORK_INFO

Since: 11

Parameters

NameDescription
NetConn_NetHandleList *netHandleListNetwork information list.

Returns

TypeDescription
int32_t0: Success. 201: Missing permissions.
401: Parameter error. 2100002: Service connection failure.
2100003: Internal error.

OHOS_NetConn_RegisterDnsResolver()

int32_t OHOS_NetConn_RegisterDnsResolver(OH_NetConn_CustomDnsResolver resolver)

Description

Registers a custom DNS resolver.

System capability: SystemCapability.Communication.NetManager.Core

Since: 11

Deprecated from: 13

Substitute: OH_NetConn_RegisterDnsResolver

Parameters

NameDescription
OH_NetConn_CustomDnsResolver resolverPointer to the custom DNS resolver.

Returns

TypeDescription
int32_t0: Success. 401: Parameter error.
2100002: Service connection failure. 2100003: Internal error.

OHOS_NetConn_UnregisterDnsResolver()

int32_t OHOS_NetConn_UnregisterDnsResolver(void)

Description

Unregisters a custom DNS resolver.

System capability: SystemCapability.Communication.NetManager.Core

Since: 11

Deprecated from: 13

Substitute: OH_NetConn_UnregisterDnsResolver

Returns

TypeDescription
int32_t0: Success.
2100002: Service connection failure.
2100003: Internal error.

OH_NetConn_RegisterDnsResolver()

int32_t OH_NetConn_RegisterDnsResolver(OH_NetConn_CustomDnsResolver resolver)

Description

Registers a custom DNS resolver.

System capability: SystemCapability.Communication.NetManager.Core

Since: 13

Parameters

NameDescription
OH_NetConn_CustomDnsResolver resolverPointer to the custom DNS resolver.

Returns

TypeDescription
int32_tResult code.
NETMANAGER_EXT_SUCCESS: Operation success.
NETMANAGER_ERR_PARAMETER_ERROR: Parameter error. Enter a correct parameter.

OH_NetConn_UnregisterDnsResolver()

int32_t OH_NetConn_UnregisterDnsResolver(void)

Description

Unregisters a custom DNS resolver.

System capability: SystemCapability.Communication.NetManager.Core

Since: 13

Returns

TypeDescription
int32_t0: Success.
2100002: Service connection failure.
2100003: Internal error.

OH_NetConn_BindSocket()

int32_t OH_NetConn_BindSocket(int32_t socketFd, NetConn_NetHandle *netHandle)

Description

Binds a socket to the specified network.

System capability: SystemCapability.Communication.NetManager.Core

Since: 12

Parameters

NameDescription
int32_t socketFdSocket constructed by the user.
NetConn_NetHandle *netHandlePointer to the network handle containing the network ID.

Returns

TypeDescription
int32_t0: Success.
401: Parameter error.
2100002: Service connection failure.
2100003: Internal error.

OH_NetConn_SetAppHttpProxy()

int32_t OH_NetConn_SetAppHttpProxy(NetConn_HttpProxy *httpProxy)

Description

Sets an HTTP proxy for the current application.

System capability: SystemCapability.Communication.NetManager.Core

Since: 12

Parameters

NameDescription
NetConn_HttpProxy *httpProxyHTTP proxy to set.

Returns

TypeDescription
int32_t0: Success.
401: Parameter error.

OH_NetConn_RegisterAppHttpProxyCallback()

int32_t OH_NetConn_RegisterAppHttpProxyCallback(OH_NetConn_AppHttpProxyChange appHttpProxyChange, uint32_t *callbackId)

Description

Registers a callback for HTTP proxy changes of the application.

System capability: SystemCapability.Communication.NetManager.Core

Since: 12

Parameters

NameDescription
OH_NetConn_AppHttpProxyChange appHttpProxyChangeCallback to register.
uint32_t *callbackIdID of the registered callback.

Returns

TypeDescription
int32_t0: Success.
401: Parameter error.

OH_NetConn_UnregisterAppHttpProxyCallback()

void OH_NetConn_UnregisterAppHttpProxyCallback(uint32_t callbackId)

Description

Unregisters the callback for HTTP proxy changes of the application.

System capability: SystemCapability.Communication.NetManager.Core

Since: 12

Parameters

NameDescription
uint32_t callbackIdID of the callback to unregister.

OH_NetConn_RegisterNetConnCallback()

int32_t OH_NetConn_RegisterNetConnCallback(NetConn_NetSpecifier *specifier, NetConn_NetConnCallback *netConnCallback,uint32_t timeout, uint32_t *callbackId)

Description

Registers a callback for network status changes.

System capability: SystemCapability.Communication.NetManager.Core

Required permissions: ohos.permission.GET_NETWORK_INFO

Since: 12

Parameters

NameDescription
netSpecifierNetwork feature set.
callbackRegistered callbacks.
uint32_t timeoutTimeout duration, in milliseconds. The value 0 indicates infinite waiting.
uint32_t *callbackIdCallback IDs.

Returns

TypeDescription
int32_t0: Success.
201: Missing permissions.
401: Parameter error.
2100002: Service connection failure.
2100003: Internal error.
2101008: Callback already registered.
2101022: Maximum number of requests exceeded.

OH_NetConn_RegisterDefaultNetConnCallback()

int32_t OH_NetConn_RegisterDefaultNetConnCallback(NetConn_NetConnCallback *netConnCallback, uint32_t *callbackId)

Description

Registers a callback for status changes of the default network.

System capability: SystemCapability.Communication.NetManager.Core

Required permissions: ohos.permission.GET_NETWORK_INFO

Since: 12

Parameters

NameDescription
callbackRegistered callbacks.
uint32_t *callbackIdCallback IDs.

Returns

TypeDescription
int32_t0: Success.
201: Missing permissions.
401: Parameter error.
2100002: Service connection failure.
2100003: Internal error.
2101008: Callback already registered.
2101022: Maximum number of requests exceeded.

OH_NetConn_UnregisterNetConnCallback()

int32_t OH_NetConn_UnregisterNetConnCallback(uint32_t callBackId)

Description

Unregisters the callback for network status changes.

System capability: SystemCapability.Communication.NetManager.Core

Required permissions: ohos.permission.GET_NETWORK_INFO

Since: 12

Parameters

NameDescription
uint32_t callBackIdID of the callback to unregister.

Returns

TypeDescription
int32_t0: Success.
201: Missing permissions.
401: Parameter error.
2100002: Service connection failure.
2100003: Internal error.
2101007: Callback not exist.

OH_NetConn_SetPacUrl()

NetConn_ErrorCode OH_NetConn_SetPacUrl(const char *pacUrl)

Description

Sets the URL of the system-level Proxy Auto Config (PAC) script, for example, http://127.0.0.1:21998/PacProxyScript.pac. You can obtain the proxy information by parsing the URL.

Required permissions: ohos.permission.SET_PAC_URL

Since: 15

Parameters

NameDescription
const char *pacUrlAddress of the PAC script.

Returns

TypeDescription
NetConn_ErrorCodeResult code defined in NetConn_ErrorCode.
NETCONN_SUCCESS: success.
NETCONN_PERMISSION_DENIED: permission denied.
NETCONN_PARAMETER_ERROR: parameter error.
NETCONN_OPERATION_FAILED: unable to connect to the service.
NETCONN_INTERNAL_ERROR: internal error.

OH_NetConn_GetPacUrl()

NetConn_ErrorCode OH_NetConn_GetPacUrl(char *pacUrl)

Description

Obtains the URL of the system-level PAC script.

Since: 15

Parameters

NameDescription
char *pacUrlURL of the PAC script.

Returns

TypeDescription
NetConn_ErrorCodeResult code defined in NetConn_ErrorCode.
NETCONN_SUCCESS: success.
NETCONN_PARAMETER_ERROR: parameter error.
NETCONN_OPERATION_FAILED: unable to connect to the service.
NETCONN_INTERNAL_ERROR: internal error.

OH_NetConn_QueryProbeResult()

int32_t OH_NetConn_QueryProbeResult(char *destination, int32_t duration, NetConn_ProbeResultInfo *probeResultInfo)

Description

Queries network probe results. If an exception (for example, network disconnection) occurs and the request fails to be sent, the API immediately returns the result without performing subsequent detection. This API involves network operations. Do not call it in the main process. Otherwise, the UI may freeze.

System capability: SystemCapability.Communication.NetManager.Core

Required permission: ohos.permission.INTERNET

Since: 20

Parameters

NameDescription
char *destinationTarget domain name or IP address to be detected. For a domain name, the domain name is resolved to the target IP address before the detection, and then the detection is initiated. The domain name resolution time is not included in the probe duration indicated by duration.
int32_t durationProbe duration. in seconds. The detection interval is 1 second. Therefore, you can use this field to control the number of detections.
NetConn_ProbeResultInfo *probeResultInfoPacket loss rate and round-trip time (RTT).

Returns

TypeDescription
int32_t0: Success.
201: Missing permissions.
401: Parameter error.
2100003: Internal error.

OH_NetConn_QueryTraceRoute()

int32_t OH_NetConn_QueryTraceRoute(char *destination, NetConn_TraceRouteOption *option,NetConn_TraceRouteInfo *traceRouteInfo)

Description

Queries network trace route information.

NOTE

To call this API, the app needs to apply for the precise location permission. According to Applying for Location Permissions (ArkTS), the caller needs to apply for both ohos.permission.APPROXIMATELY_LOCATION and ohos.permission.LOCATION.

Required permissions: ohos.permission.INTERNET, ohos.permission.LOCATION, and ohos.permission.ACCESS_NET_TRACE_INFO

Since: 20

Parameters

NameDescription
char *destinationDestination address.
NetConn_TraceRouteOption *optionRoute options.
NetConn_TraceRouteInfo *traceRouteInfoRoute result. An array pointer needs to be passed. The array size indicates the number of route hops, which is 30 by default. If you customize the number of hops, ensure that the array size is the same as the value of maxJumpNumber in the option field.

Returns

TypeDescription
int32_t0: Success.
201: Missing permissions.

你可能感兴趣的鸿蒙文章

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