harmony 鸿蒙NetConnection

2025-06-12 浏览 (1)

NetConnection

Overview

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

Since: 11

Summary

File

NameDescription
net_connection.hProvides the C APIs of the network connection module for network management.
File to include: <network/netmanager/net_connection.h>
net_connection_type.hDeclares the structs for the C APIs of the network connection module.
File to include: <network/netmanager/net_connection_type.h>

Structs

NameDescription
NetConn_NetHandleNetwork ID.
NetConn_NetCapabilitiesNetwork capability set.
NetConn_NetAddrNetwork address.
NetConn_RouteRoute configuration.
NetConn_HttpProxyProxy configuration.
NetConn_ConnectionPropertiesNetwork connection information.
NetConn_NetHandleListNetwork list.
NetConn_NetConnCallbackDefines network status callbacks.
NetConn_NetSpecifierDefines the network feature set, including a network capability set and a network ID.

Macros

NameDescription
NETCONN_MAX_NET_SIZE32
NETCONN_MAX_BEARER_TYPE_SIZE32
NETCONN_MAX_CAP_SIZE32
NETCONN_MAX_ADDR_SIZE32
NETCONN_MAX_ROUTE_SIZE64
NETCONN_MAX_EXCLUSION_SIZE256
NETCONN_MAX_STR_LEN256

Types

NameDescription
NetConn_NetCapNetwork capability set.
NetConn_NetBearerTypeNetwork carrier type.
NetConn_NetHandleNetwork ID.
NetConn_NetCapabilitiesNetwork capability set.
NetConn_NetAddrNetwork address.
NetConn_RouteRoute configuration.
NetConn_HttpProxyProxy configuration.
NetConn_ConnectionPropertiesNetwork connection information.
NetConn_NetHandleListNetwork list.
(*OH_NetConn_CustomDnsResolver) (const char *host, const char *serv, const struct addrinfo *hint, struct addrinfo **res)Pointer to the custom DNS resolver.
NetConn_NetConnCallbackDefines network status callbacks.
NetConn_NetSpecifierDefines the network feature set, including a network capability set and a network ID.
NetConn_ErrorCodeNetwork connection error code.

Enums

NameDescription
NetConn_NetCap {
NETCONN_NET_CAPABILITY_MMS = 0,
NETCONN_NET_CAPABILITY_NOT_METERED = 11,
NETCONN_NET_CAPABILITY_INTERNET = 12,
NETCONN_NET_CAPABILITY_NOT_VPN = 15,
NETCONN_NET_CAPABILITY_VALIDATED = 16
}
Network capability set.
NetConn_NetBearerType {
NETCONN_BEARER_CELLULAR = 0,
NETCONN_BEARER_WIFI = 1,
NETCONN_BEARER_ETHERNET = 3
NETCONN_BEARER_VPN = 4
}
Network carrier type.
NetConn_ErrorCode {
NETCONN_SUCCESS = 0,
NETCONN_PERMISSION_DENIED = 201,
NETCONN_PARAMETER_ERROR = 401,
NETCONN_OPERATION_FAILED = 2100002,
NETCONN_INTERNAL_ERROR= 2100003
}
Network connection error code.

Functions

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

Type Description

NetConn_ConnectionProperties

typedef struct NetConn_ConnectionProperties NetConn_ConnectionProperties

Description

Network connection information.

Since: 11

NetConn_HttpProxy

typedef struct NetConn_HttpProxy NetConn_HttpProxy

Description

Proxy configuration.

Since: 11

NetConn_NetAddr

typedef struct NetConn_NetAddr NetConn_NetAddr

Description

Network address.

Since: 11

NetConn_NetBearerType

typedef enum NetConn_NetBearerType NetConn_NetBearerType

Description

Network carrier type.

Since: 11

NetConn_NetCap

typedef enum NetConn_NetCap NetConn_NetCap

Description

Network capability set.

Since: 11

NetConn_NetCapabilities

typedef struct NetConn_NetCapabilities NetConn_NetCapabilities

Description

Network capability set.

Since: 11

NetConn_NetHandle

typedef struct NetConn_NetHandle NetConn_NetHandle

Description

Network ID.

Since: 11

NetConn_NetHandleList

typedef struct NetConn_NetHandleList NetConn_NetHandleList

Description

Network list.

Since: 11

NetConn_Route

typedef struct NetConn_Route NetConn_Route

Description

Route configuration.

Since: 11

OH_NetConn_CustomDnsResolver

typedef int(* OH_NetConn_CustomDnsResolver) (const char *host, const char *serv, const struct addrinfo *hint, struct addrinfo **res)

Description

Pointer to the custom DNS resolver.

Since: 11

Parameters

NameDescription
hostHost name.
servService name.
hintPointer to the addrinfo structure.
resDNS query result, which is in the format of linked lists.

OH_NetConn_AppHttpProxyChange

typedef void(* OH_NetConn_AppHttpProxyChange) (NetConn_HttpProxy *proxy)

Description

Defines the callback for application-level proxy changes.

Since: 12

Parameters

NameDescription
proxyProxy configuration information (probably a null pointer).

NetConn_NetConnCallback

typedef struct NetConn_NetConnCallback NetConn_NetConnCallback

Description

Defines network status callbacks.

Since: 12

NetConn_NetSpecifier

typedef struct NetConn_NetSpecifier NetConn_NetSpecifier

Description

Defines the network feature set, including a network capability set and a network ID.

Since: 12

NetConn_ErrorCode

typedef enum NetConn_ErrorCode NetConn_ErrorCode

Description

Enumerates network connection error codes.

Since: 15

Enum Description

NetConn_NetBearerType

enum NetConn_NetBearerType

Description

Network carrier type.

Since: 11

ValueDescription
NETCONN_BEARER_CELLULARCellular network.
NETCONN_BEARER_WIFIWi-Fi.
NETCONN_BEARER_BLUETOOTHBluetooth.
Since: 12
NETCONN_BEARER_ETHERNETEthernet.
NETCONN_BEARER_VPNVirtual private network.
Since: 12

NetConn_NetCap

enum NetConn_NetCap

Description

Network capability set.

Since: 11

ValueDescription
NETCONN_NET_CAPABILITY_MMSMMS.
NETCONN_NET_CAPABILITY_NOT_METEREDNon-metered network.
NETCONN_NET_CAPABILITY_INTERNETInternet.
NETCONN_NET_CAPABILITY_NOT_VPNNon-VPN.
NETCONN_NET_CAPABILITY_VALIDATEDVerified.
NETCONN_NET_CAPABILITY_CHECKING_CONNECTIVITYConnectivity check.
Since: 12

NetConn_ErrorCode

enum NetConn_ErrorCode

Description

Enumerates network connection error codes.

Since: 15

ValueDescription
NETCONN_SUCCESSSuccess.
NETCONN_PERMISSION_DENIEDPermission verification failed.
NETCONN_PARAMETER_ERRORParameter check failed.
NETCONN_OPERATION_FAILEDService connection failed.
NETCONN_INTERNAL_ERRORInternal server error.

Function Description

OH_NetConn_FreeDnsResult()

int32_t OH_NetConn_FreeDnsResult (struct addrinfo * res)

Description

Releases the DNS query result.

System capability: SystemCapability.Communication.NetManager.Core

Since: 11

Parameters

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

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

Required permissions:

ohos.permission.INTERNET

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 using netId.

System capability: SystemCapability.Communication.NetManager.Core

Since: 11

Parameters

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

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

Required permissions:

ohos.permission.INTERNET

OH_NetConn_GetAllNets()

int32_t OH_NetConn_GetAllNets (NetConn_NetHandleList * netHandleList)

Description

Obtains all activated data networks.

System capability: SystemCapability.Communication.NetManager.Core

Since: 11

Parameters

NameDescription
netHandleListNetwork information list.

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

Required permissions:

ohos.permission.GET_NETWORK_INFO

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

Since: 11

Parameters

NameDescription
nethandleNetwork ID.
propLink information.

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

Required permissions:

ohos.permission.GET_NETWORK_INFO

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
httpProxyProxy configuration.

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

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

Since: 11

Parameters

NameDescription
netHandleNetwork ID.

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

Required permissions:

ohos.permission.GET_NETWORK_INFO

OH_NetConn_GetNetCapabilities()

int32_t OH_NetConn_GetNetCapabilities (NetConn_NetHandle * netHandle, NetConn_NetCapabilities * netCapacities )

Description

Obtains the capabilities of a data network.

System capability: SystemCapability.Communication.NetManager.Core

Since: 11

Parameters

NameDescription
netHandleNetwork ID.
netCapacitiesCapability set.

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

Required permissions:

ohos.permission.GET_NETWORK_INFO

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

Since: 11

Parameters

NameDescription
hasDefaultNetWhether there is a default network.

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

Required permissions:

ohos.permission.GET_NETWORK_INFO

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

Since: 11

Parameters

NameDescription
isMeteredWhether metering is enabled.

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

Required permissions:

ohos.permission.GET_NETWORK_INFO

OHOS_NetConn_RegisterDnsResolver() (deprecated)

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 API: OH_NetConn_RegisterDnsResolver

Parameters

NameDescription
resolverPointer to the custom DNS resolver.

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

Required permissions:

ohos.permission.INTERNET

OHOS_NetConn_UnregisterDnsResolver() (deprecated)

int32_t OHOS_NetConn_UnregisterDnsResolver (void )

Description

Unregisters a custom DNS resolver.

System capability: SystemCapability.Communication.NetManager.Core

Since: 11

Deprecated from: 13

Substitute API: OH_NetConn_UnregisterDnsResolver

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

Required permissions:

ohos.permission.INTERNET

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
resolverPointer to the custom DNS resolver.

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

Required permissions:

ohos.permission.INTERNET

OH_NetConn_UnregisterDnsResolver()

int32_t OH_NetConn_UnregisterDnsResolver (void )

Description

Unregisters a custom DNS resolver.

System capability: SystemCapability.Communication.NetManager.Core

Since: 13

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

Required permissions:

ohos.permission.INTERNET

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
socketFdFile descriptor of the socket created by the user.
netHandleNetwork ID.

Returns

0: success.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

OH_NetConn_SetAppHttpProxy()

int32_t OH_NetConn_SetAppHttpProxy(NetConn_HttpProxy *httpProxy)

Description

Sets the application-level proxy configuration.

System capability: SystemCapability.Communication.NetManager.Core

Since: 12

Parameters

NameDescription
httpProxyApplication-level proxy configuration.

Returns

0: success.

401: parameter error.

OH_NetConn_RegisterAppHttpProxyCallback()

int32_t OH_NetConn_RegisterAppHttpProxyCallback(OH_NetConn_AppHttpProxyChange appHttpProxyChange, uint32_t *callbackId)

Description

Registers a callback for application-level proxy changes.

System capability: SystemCapability.Communication.NetManager.Core

Since: 12

Parameters

NameDescription
appHttpProxyChangePointer to the callback for application-level proxy changes.
callbackIdCallback ID.

Returns

0: success.

401: parameter error.

OH_NetConn_UnregisterAppHttpProxyCallback()

void OH_NetConn_UnregisterAppHttpProxyCallback(uint32_t callbackId)

Description

Unregisters the callback for application-level proxy changes.

System capability: SystemCapability.Communication.NetManager.Core

Since: 12

Parameters

NameDescription
callbackIdCallback ID, which is obtained during registration.

OH_NetConn_RegisterNetConnCallback()

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

Description

Registers a callback to for status changes of the specified network.

System capability: SystemCapability.Communication.NetManager.Core

Since: 12

Parameters

NameDescription
specifierNetwork feature set.
netConnCallbackRegistered callbacks.
timeoutWaiting time, in milliseconds. The value 0 indicates infinite waiting.
callbackIdCallback IDs.

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

2101008: The callback has been registered.

2101022: The maximum number of requests is exceeded.

Required permissions:

ohos.permission.GET_NETWORK_INFO

OH_NetConn_RegisterDefaultNetConnCallback()

int32_t OH_NetConn_RegisterDefaultNetConnCallback(NetConn_NetConnCallback *netConnCallback, uint32_t *callbackId)

Description

Registers a callback to for status changes of the default network.

System capability: SystemCapability.Communication.NetManager.Core

Since: 12

Parameters

NameDescription
netConnCallbackRegistered callbacks.
callbackIdCallback IDs.

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

2101008: The callback has been registered.

2101022: The maximum number of requests is exceeded.

Required permissions:

ohos.permission.GET_NETWORK_INFO

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

Since: 12

Parameters

NameDescription
callBackIdCallback ID.

Returns

0: success.

201: no permission.

401: parameter error.

2100002: failed to connect to the service.

2100003: internal error.

2101007: The corresponding callback is not found.

Required permissions:

ohos.permission.GET_NETWORK_INFO

OH_NetConn_SetPacUrl()

NetConn_ErrorCode OH_NetConn_SetPacUrl(const char *pacUrl)

Description

Sets the URL of the system-level proxy auto-config (PAC) script.

System capability: SystemCapability.Communication.NetManager.Core

Since: 15

Parameters

NameDescription
pacUrlURL of the PAC script. Note that this URL will not be verified by the API.

Returns

NETCONN_SUCCESS: success.

NETCONN_PERMISSION_DENIED: no permission.

NETCONN_PARAMETER_ERROR: invalid parameter.

NETCONN_OPERATION_FAILED: service connection failed.

NETCONN_INTERNAL_ERROR: internal error.

Required permissions:

ohos.permission.SET_PAC_URL

OH_NetConn_GetPacUrl()

NetConn_ErrorCode OH_NetConn_GetPacUrl(char *pacUrl)

Description

Obtains the URL of the system-level PAC script.

System capability: SystemCapability.Communication.NetManager.Core

Since: 15

Parameters

NameDescription
pacUrlURL of the PAC script.

Returns

NETCONN_SUCCESS: success.

NETCONN_PERMISSION_DENIED: no permission.

NETCONN_OPERATION_FAILED: service connection failed.

NETCONN_INTERNAL_ERROR: internal error.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Network Kit

harmony 鸿蒙NetConn_ConnectionProperties

harmony 鸿蒙NetConn_HttpProxy

harmony 鸿蒙NetConn_NetAddr

harmony 鸿蒙NetConn_NetCapabilities

harmony 鸿蒙NetConn_NetConnCallback

harmony 鸿蒙NetConn_NetHandle

harmony 鸿蒙NetConn_NetHandleList

harmony 鸿蒙NetConn_NetSpecifier

harmony 鸿蒙NetConn_Route

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