1# @ohos.net.connection (Network Connection Management) 2 3The network connection management module provides basic network management capabilities. You can obtain the default active data network or the list of all active data networks, enable or disable the airplane mode, and obtain network capability information. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> Unless otherwise specified, the APIs of this module do no support concurrent calls. 9 10## Modules to Import 11 12```ts 13import { connection } from '@kit.NetworkKit'; 14``` 15 16## connection.createNetConnection 17 18createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection 19 20Creates a **NetConnection** object, where [netSpecifier](#netspecifier) specifies the network, and **timeout** specifies the timeout duration in ms. **timeout** is configurable only when **netSpecifier** is specified. If neither of them is present, the default network is used. 21 22**Atomic service API**: This API can be used in atomic services since API version 11. 23 24**System capability**: SystemCapability.Communication.NetManager.Core 25 26**Parameters** 27 28| Name | Type | Mandatory| Description | 29| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ | 30| netSpecifier | [NetSpecifier](#netspecifier) | No | Network specifier, which specifies the characteristics of a network. If this parameter is not set or is set to **undefined**, the default network is used. | 31| timeout | number | No | Timeout duration for obtaining the network specified by **netSpecifier**. This parameter is valid only when **netSpecifier** is specified. The default value is **0** if **netSpecifier** is **undefined**.| 32 33**Return value** 34 35| Type | Description | 36| ------------------------------- | -------------------- | 37| [NetConnection](#netconnection) | Handle of the network specified by **netSpecifier**.| 38 39**Example** 40 41```ts 42import { connection } from '@kit.NetworkKit'; 43 44// For the default network, you do not need to pass in parameters. 45let netConnection = connection.createNetConnection(); 46 47// For the cellular network, you need to pass in related network parameters. If the timeout parameter is not specified, the timeout value is 0 by default. 48let netConnectionCellular = connection.createNetConnection({ 49 netCapabilities: { 50 bearerTypes: [connection.NetBearType.BEARER_CELLULAR] 51 } 52}); 53``` 54 55## connection.getDefaultNet 56 57getDefaultNet(callback: AsyncCallback\<NetHandle>): void 58 59Obtains the default active data network. This API uses an asynchronous callback to return the result. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities. 60 61**Required permission**: ohos.permission.GET_NETWORK_INFO 62 63**Atomic service API**: This API can be used in atomic services since API version 11. 64 65**System capability**: SystemCapability.Communication.NetManager.Core 66 67**Parameters** 68 69| Name | Type | Mandatory| Description | 70| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 71| callback | AsyncCallback\<[NetHandle](#nethandle)> | Yes | Callback used to return the result. If the default activated data network is obtained successfully, **error** is **undefined** and **data** is the default activated data network. Otherwise, **error** is an error object.| 72 73**Error codes** 74 75For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 76 77| ID| Error Message | 78| ------- | ----------------------------- | 79| 201 | Permission denied. | 80| 401 | Parameter error. | 81| 2100002 | Failed to connect to the service. | 82| 2100003 | System internal error. | 83 84**Example** 85 86```ts 87import { connection } from '@kit.NetworkKit'; 88import { BusinessError } from '@kit.BasicServicesKit'; 89 90connection.getDefaultNet((error: BusinessError, data: connection.NetHandle) => { 91 if (error) { 92 console.error(`Failed to get default net. Code:${error.code}, message:${error.message}`); 93 return; 94 } 95 console.info("Succeeded to get data " + JSON.stringify(data)); 96}); 97``` 98 99## connection.getDefaultNet 100 101getDefaultNet(): Promise\<NetHandle> 102 103Obtains the default active data network. This API uses a promise to return the result. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities. 104 105**Required permission**: ohos.permission.GET_NETWORK_INFO 106 107**Atomic service API**: This API can be used in atomic services since API version 11. 108 109**System capability**: SystemCapability.Communication.NetManager.Core 110 111**Return value** 112 113| Type | Description | 114| --------------------------------- | ------------------------------------- | 115| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.| 116 117**Error codes** 118 119For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 120 121| ID| Error Message | 122| ------- | -------------------------------- | 123| 201 | Permission denied. | 124| 2100002 | Failed to connect to the service.| 125| 2100003 | System internal error. | 126 127**Example** 128 129```ts 130import { connection } from '@kit.NetworkKit'; 131 132connection.getDefaultNet().then((data: connection.NetHandle) => { 133 console.info("Succeeded to get data: " + JSON.stringify(data)); 134}); 135``` 136 137## connection.getDefaultNetSync<sup>9+</sup> 138 139getDefaultNetSync(): NetHandle 140 141Obtains the default active data network in synchronous mode. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities. 142 143**Required permission**: ohos.permission.GET_NETWORK_INFO 144 145**Atomic service API**: This API can be used in atomic services since API version 11. 146 147**System capability**: SystemCapability.Communication.NetManager.Core 148 149**Return value** 150 151| Type | Description | 152| --------- | ---------------------------------- | 153| [NetHandle](#nethandle) | Handle of the default active data network.| 154 155**Error codes** 156 157For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 158 159| ID| Error Message | 160| ------- | -------------------------------- | 161| 201 | Permission denied. | 162| 2100002 | Failed to connect to the service.| 163| 2100003 | System internal error. | 164 165**Example** 166 167```ts 168import { connection } from '@kit.NetworkKit'; 169 170let netHandle = connection.getDefaultNetSync(); 171``` 172 173 174## connection.setAppHttpProxy<sup>11+</sup> 175 176setAppHttpProxy(httpProxy: HttpProxy): void 177 178Sets the application-level HTTP proxy configuration of the network. 179 180**System capability**: SystemCapability.Communication.NetManager.Core 181 182**Parameters** 183 184| Name | Type | Mandatory| Description | 185| --------- | ------------------------------------------------------------ | ---- | ---------------- | 186| httpProxy | [HttpProxy](#httpproxy10) | Yes | Application-level HTTP proxy configuration.| 187 188**Error codes** 189 190For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 191 192| ID| Error Message | 193| ------- | ----------------------------- | 194| 401 | Parameter error. | 195| 2100001 | Invalid http proxy. | 196 197**Example** 198 199```ts 200import { connection } from '@kit.NetworkKit'; 201import { BusinessError } from '@kit.BasicServicesKit'; 202 203let exclusionStr = "192.168,baidu.com"; 204let exclusionArray = exclusionStr.split(','); 205connection.setAppHttpProxy({ 206 host: "192.168.xx.xxx", 207 port: 8080, 208 exclusionList: exclusionArray 209} as connection.HttpProxy); 210``` 211 212**Preset certificate PIN:** 213 214A certificate PIN is the hash value calculated using the SHA256 algorithm for a certificate file. 215For the **server.pem** certificate, you can use the following openssl command to calculate its PIN: 216 217```shell 218cat server.pem \ 219| sed -n '/-----BEGIN/,/-----END/p' \ 220| openssl x509 -noout -pubkey \ 221| openssl pkey -pubin -outform der \ 222| openssl dgst -sha256 -binary \ 223| openssl enc -base64 224``` 225 226**Preset application-level certificate:** 227 228The original certificate file is preset in the application. Currently, certificate files in the **.crt** and **.pem** formats are supported. 229 230**NOTE** 231 232Currently, certificate pinning has been enabled for the ohos.net.http and Image components, and the hash values of all certificates in the certificate chain are matched. If any certificate is updated on the server, the verification fails. Therefore, if any certificate on the server has been updated, upgrade the application to the latest version as soon as possible. Otherwise, network connection may fail. 233 234**Preset JSON configuration file:** 235 236The mapping between preset certificates and network servers is configured in a JSON configuration file. 237The configuration file is stored in the **src/main/resources/base/profile/network_config.json** directory of the application. 238 239**JSON configuration file:** 240 241The following is an example configuration of the certificate pin: 242```json 243{ 244 "network-security-config": { 245 "domain-config": [ 246 { 247 "domains": [ 248 { 249 "include-subdomains": true, 250 "name": "server.com" 251 } 252 ], 253 "pin-set": { 254 "expiration": "2024-11-08", 255 "pin": [ 256 { 257 "digest-algorithm": "sha256", 258 "digest": "FEDCBA987654321" 259 } 260 ] 261 } 262 } 263 ] 264 }, 265 "trust-global-user-ca": false, 266 "trust-current-user-ca": false, 267} 268``` 269 270The following is an example configuration of the application-level certificate: 271```json 272{ 273 "network-security-config": { 274 "base-config": { 275 "trust-anchors": [ 276 { 277 "certificates": "/etc/security/certificates" 278 } 279 ] 280 }, 281 "domain-config": [ 282 { 283 "domains": [ 284 { 285 "include-subdomains": true, 286 "name": "example.com" 287 } 288 ], 289 "trust-anchors": [ 290 { 291 "certificates": "/data/storage/el1/bundle/entry/resources/resfile" 292 } 293 ] 294 } 295 ] 296 } 297} 298 299``` 300 301**Description of fields** 302 303**network-security-config (object: network security configuration)** 304 305This field can contain zero or one **base-config**. 306 307This field must contain one **domain-config**. 308 309**trust-global-user-ca**: This field specifies whether to trust the CA certificate manually installed by the enterprise MDM system or device administrator. The default value is **true**. 310 311**trust-current-user-ca**: This field specifies whether to trust the certificate installed by the current user. The default value is **true**. 312 313**base-config (object: application-wide security configuration)** 314 315This field must contain one **trust-anchors**. 316 317**domain-config (array: security configuration of each domain)** 318 319This field can contain any number of items. 320 321An item must contain one **domain**. 322 323An item can contain zero or one **trust-anchors**. 324 325An item can contain zero or one **pin-set**. 326 327**trust-anchors (array: trusted CA)** 328 329This field can contain any number of items. 330 331An item must contain one **certificates** (string: CA certificate path). 332 333**domain (array: domain)** 334 335This field can contain any number of items. 336 337An item must contain one **name** (string: domain name). 338 339An item can contain zero or one **include-subdomains** (boolean: whether a rule is applicable to subdomains). 340 341**pin-set (object: certificate PIN setting)** 342 343This field must contain one **pin**. 344 345This field can contain zero or one **expiration** (string: expiration time of the certificate PIN). 346 347**pin (array: certificate PIN)** 348 349This field can contain any number of items. 350 351An item must contain one **digest-algorithm** (string: digest algorithm used to generate the PIN). 352 353An item must contain one **digest** (string: public key PIN). 354 355## connection.getDefaultHttpProxy<sup>10+</sup> 356 357getDefaultHttpProxy(callback: AsyncCallback\<HttpProxy>): void 358 359Obtains the default HTTP proxy configuration of the network. 360If the global proxy is set, the global HTTP proxy configuration is returned. If [setAppNet](#connectionsetappnet9) is used to bind the application to the network specified by [NetHandle](#nethandle), the HTTP proxy configuration of this network is returned. In other cases, the HTTP proxy configuration of the default network is returned. 361This API uses an asynchronous callback to return the result. 362 363**System capability**: SystemCapability.Communication.NetManager.Core 364 365**Parameters** 366 367| Name | Type | Mandatory| Description | 368| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 369| callback | AsyncCallback<[HttpProxy](#httpproxy10)> | Yes | Callback used to return the result. If the global HTTP proxy configuration of the network is obtained successfully, **error** is **undefined** and **data** is the global HTTP proxy configuration. Otherwise, **error** is an error object.| 370 371**Error codes** 372 373For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 374 375| ID| Error Message | 376| -------- | -------------------------------------------- | 377| 2100002 | Failed to connect to the service. | 378| 2100003 | System internal error. | 379 380**Example** 381 382```ts 383import { connection } from '@kit.NetworkKit'; 384import { BusinessError } from '@kit.BasicServicesKit'; 385 386connection.getDefaultHttpProxy((error: BusinessError, data: connection.HttpProxy) => { 387 if (error) { 388 console.error(`Failed to get default http proxy. Code:${error.code}, message:${error.message}`); 389 return; 390 } 391 console.log("Succeeded to get data" + JSON.stringify(data)); 392}); 393``` 394 395## connection.getDefaultHttpProxy<sup>10+</sup> 396 397getDefaultHttpProxy(): Promise\<HttpProxy> 398 399Obtains the default HTTP proxy configuration of the network. 400If the global proxy is set, the global HTTP proxy configuration is returned. If [setAppNet](#connectionsetappnet9) is used to bind the application to the network specified by [NetHandle](#nethandle), the HTTP proxy configuration of this network is returned. In other cases, the HTTP proxy configuration of the default network is returned. 401This API uses a promise to return the result. 402 403**System capability**: SystemCapability.Communication.NetManager.Core 404 405**Return value** 406 407| Type | Description | 408| -------------------------------- | ----------------------------------------- | 409| Promise<[HttpProxy](#httpproxy10)> | Promise used to return the result.| 410 411**Error codes** 412 413For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 414 415| ID| Error Message | 416| -------- | -------------------------------------------- | 417| 2100002 | Failed to connect to the service. | 418| 2100003 | System internal error. | 419 420**Example** 421 422```ts 423import { connection } from '@kit.NetworkKit'; 424import { BusinessError } from '@kit.BasicServicesKit'; 425 426connection.getDefaultHttpProxy().then((data: connection.HttpProxy) => { 427 console.info(JSON.stringify(data)); 428}).catch((error: BusinessError) => { 429 console.info(JSON.stringify(error)); 430}); 431``` 432 433## connection.getAppNet<sup>9+</sup> 434 435getAppNet(callback: AsyncCallback\<NetHandle>): void 436 437Obtains information about the network bound to an application. This API uses an asynchronous callback to return the result. 438 439**System capability**: SystemCapability.Communication.NetManager.Core 440 441**Parameters** 442 443| Name | Type | Mandatory| Description | 444| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 445| callback | AsyncCallback\<[NetHandle](#nethandle)> | Yes | Callback used to return the result. If information about the network bound to the application is successfully obtained, **error** is **undefined** and **data** is the obtained network information. Otherwise, **error** is an error object.| 446 447**Error codes** 448 449For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 450 451| ID| Error Message | 452| ------- | ----------------------------- | 453| 401 | Parameter error. | 454| 2100002 | Failed to connect to the service.| 455| 2100003 | System internal error. | 456 457**Example** 458 459```ts 460import { connection } from '@kit.NetworkKit'; 461import { BusinessError } from '@kit.BasicServicesKit'; 462 463connection.getAppNet((error: BusinessError, data: connection.NetHandle) => { 464 if (error) { 465 console.error(`Failed to get app net. Code:${error.code}, message:${error.message}`); 466 return; 467 } 468 console.info("Succeeded to get data: " + JSON.stringify(data)); 469}) 470``` 471 472## connection.getAppNet<sup>9+</sup> 473 474getAppNet(): Promise\<NetHandle> 475 476Obtains information about the network bound to an application. This API uses a promise to return the result. 477 478**System capability**: SystemCapability.Communication.NetManager.Core 479 480**Return value** 481 482| Type | Description | 483| --------------------------------- | ------------------------------------- | 484| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.| 485 486**Error codes** 487 488For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 489 490| ID| Error Message | 491| ------- | ----------------------------- | 492| 2100002 | Failed to connect to the service.| 493| 2100003 | System internal error. | 494 495**Example** 496 497```ts 498import { connection } from '@kit.NetworkKit'; 499import { BusinessError } from '@kit.BasicServicesKit'; 500 501connection.getAppNet().then((data: connection.NetHandle) => { 502 console.info(JSON.stringify(data)); 503}).catch((error: BusinessError) => { 504 console.info(JSON.stringify(error)); 505}); 506``` 507 508## connection.getAppNetSync<sup>10+</sup> 509 510getAppNetSync(): NetHandle 511 512Obtains information about the network bound to an application. This API returns the result synchronously. 513 514**System capability**: SystemCapability.Communication.NetManager.Core 515 516**Return value** 517 518| Type | Description | 519| --------- | ---------------------------------- | 520| [NetHandle](#nethandle) | Handle of the data network bound to the application.| 521 522**Error codes** 523 524For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 525 526| ID| Error Message | 527| ------- | ----------------------------- | 528| 2100002 | Failed to connect to the service.| 529| 2100003 | System internal error. | 530 531**Example** 532 533```ts 534import { connection } from '@kit.NetworkKit'; 535 536let netHandle = connection.getAppNetSync(); 537``` 538 539## connection.setAppNet<sup>9+</sup> 540 541setAppNet(netHandle: NetHandle, callback: AsyncCallback\<void>): void 542 543Binds an application to the specified network, so that the application can access the external network only through this network. This API uses an asynchronous callback to return the result. 544 545**Required permissions**: ohos.permission.INTERNET 546 547**System capability**: SystemCapability.Communication.NetManager.Core 548 549**Parameters** 550 551| Name | Type | Mandatory| Description | 552| --------- | ----------------------- | ---- | ------------------------------------------------------------ | 553| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network. | 554| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the application is successfully bound to the specified network, **error** is **undefined**. Otherwise, **error** is an error object.| 555 556**Error codes** 557 558For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 559 560| ID| Error Message | 561| ------- | ----------------------------- | 562| 201 | Permission denied. | 563| 401 | Parameter error. | 564| 2100001 | Invalid parameter value. | 565| 2100002 | Failed to connect to the service.| 566| 2100003 | System internal error. | 567 568**Example** 569 570```ts 571import { connection } from '@kit.NetworkKit'; 572import { BusinessError } from '@kit.BasicServicesKit'; 573 574connection.getDefaultNet((error: BusinessError, netHandle: connection.NetHandle) => { 575 if (netHandle.netId == 0) { 576 // If no network is connected, the obtained netid of netHandler is 0, which is abnormal. You can add specific processing based on the service requirements. 577 return; 578 } 579 connection.setAppNet(netHandle, (error: BusinessError, data: void) => { 580 if (error) { 581 console.error(`Failed to get default net. Code:${error.code}, message:${error.message}`); 582 return; 583 } 584 console.info("Succeeded to get data: " + JSON.stringify(data)); 585 }); 586}); 587``` 588 589## connection.setAppNet<sup>9+</sup> 590 591setAppNet(netHandle: NetHandle): Promise\<void> 592 593Binds an application to the specified network, so that the application can access the external network only through this network. This API uses a promise to return the result. 594 595**Required permissions**: ohos.permission.INTERNET 596 597**System capability**: SystemCapability.Communication.NetManager.Core 598 599**Parameters** 600 601| Name | Type | Mandatory| Description | 602| --------- | ------------------------------------------------------------ | ---- | ---------------- | 603| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 604 605**Return value** 606 607| Type | Description | 608| ------------------------------------------- | ----------------------------- | 609| Promise\<void> | Promise that returns no value.| 610 611**Error codes** 612 613For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 614 615| ID| Error Message | 616| ------- | ----------------------------- | 617| 201 | Permission denied. | 618| 401 | Parameter error. | 619| 2100001 | Invalid parameter value. | 620| 2100002 | Failed to connect to the service.| 621| 2100003 | System internal error. | 622 623**Example** 624 625```ts 626import { connection } from '@kit.NetworkKit'; 627import { BusinessError } from '@kit.BasicServicesKit'; 628 629connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 630 if (netHandle.netId == 0) { 631 // If no network is connected, the obtained netid of netHandler is 0, which is abnormal. You can add specific processing based on the service requirements. 632 return; 633 } 634 635 connection.setAppNet(netHandle).then(() => { 636 console.log("success"); 637 }).catch((error: BusinessError) => { 638 console.log(JSON.stringify(error)); 639 }) 640}); 641``` 642 643## connection.getAllNets 644 645getAllNets(callback: AsyncCallback<Array<NetHandle>>): void 646 647Obtains the list of all connected networks. This API uses an asynchronous callback to return the result. 648 649**Required permission**: ohos.permission.GET_NETWORK_INFO 650 651**System capability**: SystemCapability.Communication.NetManager.Core 652 653**Parameters** 654 655| Name| Type| Mandatory| Description| 656| -------- | -------- | -------- | -------- | 657| callback | AsyncCallback<Array<[NetHandle](#nethandle)>> | Yes| Callback used to return the result. If the list of all connected networks is obtained successfully, **error** is **undefined** and **data** is the list of activated data networks. Otherwise, **error** is an error object.| 658 659**Error codes** 660 661For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 662 663| ID| Error Message | 664| ------- | ----------------------------- | 665| 201 | Permission denied. | 666| 401 | Parameter error. | 667| 2100002 | Failed to connect to the service.| 668| 2100003 | System internal error. | 669 670**Example** 671 672```ts 673import { connection } from '@kit.NetworkKit'; 674import { BusinessError } from '@kit.BasicServicesKit'; 675 676connection.getAllNets((error: BusinessError, data: connection.NetHandle[]) => { 677 if (error) { 678 console.error(`Failed to get all nets. Code:${error.code}, message:${error.message}`); 679 return; 680 } 681 console.info("Succeeded to get data: " + JSON.stringify(data)); 682}); 683``` 684 685## connection.getAllNets 686 687getAllNets(): Promise<Array<NetHandle>> 688 689Obtains the list of all connected networks. This API uses a promise to return the result. 690 691**Required permission**: ohos.permission.GET_NETWORK_INFO 692 693**System capability**: SystemCapability.Communication.NetManager.Core 694 695**Return value** 696 697| Type| Description| 698| -------- | -------- | 699| Promise<Array<[NetHandle](#nethandle)>> | Promise used to return the result.| 700 701**Error codes** 702 703For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 704 705| ID| Error Message | 706| ------- | ----------------------------- | 707| 201 | Permission denied. | 708| 2100002 | Failed to connect to the service.| 709| 2100003 | System internal error. | 710 711**Example** 712 713```ts 714import { connection } from '@kit.NetworkKit'; 715 716connection.getAllNets().then((data: connection.NetHandle[]) => { 717 console.info("Succeeded to get data: " + JSON.stringify(data)); 718}); 719``` 720 721## connection.getAllNetsSync<sup>10+</sup> 722 723getAllNetsSync(): Array<NetHandle> 724 725Obtains the list of all connected networks. This API returns the result synchronously. 726 727**Required permission**: ohos.permission.GET_NETWORK_INFO 728 729**System capability**: SystemCapability.Communication.NetManager.Core 730 731**Return value** 732 733| Type | Description | 734| --------- | ---------------------------------- | 735| Array<[NetHandle](#nethandle)> | List of all activated data networks.| 736 737**Error codes** 738 739For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 740 741| ID| Error Message | 742| ------- | ----------------------------- | 743| 201 | Permission denied. | 744| 2100002 | Failed to connect to the service.| 745| 2100003 | System internal error. | 746 747**Example** 748 749```ts 750import { connection } from '@kit.NetworkKit'; 751 752let netHandle = connection.getAllNetsSync(); 753``` 754 755## connection.getConnectionProperties 756 757getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<ConnectionProperties>): void 758 759Obtains connection properties of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result. 760 761**Required permission**: ohos.permission.GET_NETWORK_INFO 762 763**System capability**: SystemCapability.Communication.NetManager.Core 764 765**Parameters** 766 767| Name | Type | Mandatory| Description | 768| --------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 769| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network. | 770| callback | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | Yes | Callback used to return the result. If the connection properties of the network corresponding to the **netHandle** is obtained successfully, **error** is **undefined** and **data** is the obtained network connection information. Otherwise, **error** is an error object.| 771 772**Error codes** 773 774For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 775 776| ID| Error Message | 777| ------- | ----------------------------- | 778| 201 | Permission denied. | 779| 401 | Parameter error. | 780| 2100001 | Invalid parameter value. | 781| 2100002 | Failed to connect to the service.| 782| 2100003 | System internal error. | 783 784**Example** 785 786```ts 787import { connection } from '@kit.NetworkKit'; 788import { BusinessError } from '@kit.BasicServicesKit'; 789 790connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 791 if (netHandle.netId == 0) { 792 // If no network is connected, the obtained netid of netHandler is 0, which is abnormal. You can add specific processing based on the service requirements. 793 return; 794 } 795 connection.getConnectionProperties(netHandle, (error: BusinessError, data: connection.ConnectionProperties) => { 796 if (error) { 797 console.error(`Failed to get connection properties. Code:${error.code}, message:${error.message}`); 798 return; 799 } 800 console.info("Succeeded to get data: " + JSON.stringify(data)); 801 }) 802}); 803``` 804 805## connection.getConnectionProperties 806 807getConnectionProperties(netHandle: NetHandle): Promise\<ConnectionProperties> 808 809Obtains connection properties of the network corresponding to the **netHandle**. This API uses a promise to return the result. 810 811**Required permission**: ohos.permission.GET_NETWORK_INFO 812 813**System capability**: SystemCapability.Communication.NetManager.Core 814 815**Parameters** 816 817| Name | Type | Mandatory| Description | 818| --------- | ----------------------- | ---- | ---------------- | 819| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 820 821**Return value** 822 823| Type | Description | 824| ------------------------------------------------------- | --------------------------------- | 825| Promise\<[ConnectionProperties](#connectionproperties)> | Promise used to return the result.| 826 827**Error codes** 828 829For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 830 831| ID| Error Message | 832| ------- | ----------------------------- | 833| 201 | Permission denied. | 834| 401 | Parameter error. | 835| 2100001 | Invalid parameter value. | 836| 2100002 | Failed to connect to the service.| 837| 2100003 | System internal error. | 838 839**Example** 840 841```ts 842import { connection } from '@kit.NetworkKit'; 843 844connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 845 if (netHandle.netId == 0) { 846 // If no network is connected, the obtained netid of netHandler is 0, which is abnormal. You can add specific processing based on the service requirements. 847 return; 848 } 849 850 connection.getConnectionProperties(netHandle).then((data: connection.ConnectionProperties) => { 851 console.info("Succeeded to get data: " + JSON.stringify(data)); 852 }) 853}); 854``` 855 856## connection.getConnectionPropertiesSync<sup>10+</sup> 857 858getConnectionPropertiesSync(netHandle: NetHandle): ConnectionProperties 859 860Obtains network connection information based on the specified **netHandle**. 861 862**Required permission**: ohos.permission.GET_NETWORK_INFO 863 864**System capability**: SystemCapability.Communication.NetManager.Core 865 866**Parameters** 867 868| Name | Type | Mandatory| Description | 869| --------- | ----------------------- | ---- | ---------------- | 870| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 871 872**Return value** 873 874| Type | Description | 875| ------------------------------------------------------- | --------------------------------- | 876| [ConnectionProperties](#connectionproperties) | Network connection information.| 877 878**Error codes** 879 880For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 881 882| ID| Error Message | 883| ------- | ----------------------------- | 884| 201 | Permission denied. | 885| 401 | Parameter error. | 886| 2100001 | Invalid parameter value. | 887| 2100002 | Failed to connect to the service.| 888| 2100003 | System internal error. | 889 890**Example** 891 892```ts 893import { connection } from '@kit.NetworkKit'; 894import { BusinessError } from '@kit.BasicServicesKit'; 895 896let netHandle: connection.NetHandle; 897let connectionproperties: connection.ConnectionProperties; 898 899connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 900 if (netHandle.netId == 0) { 901 // If no network is connected, the obtained netid of netHandler is 0, which is abnormal. You can add specific processing based on the service requirements. 902 return; 903 } 904 netHandle = connection.getDefaultNetSync(); 905 connectionproperties = connection.getConnectionPropertiesSync(netHandle); 906 console.info("Succeeded to get connectionproperties: " + JSON.stringify(connectionproperties)); 907}); 908 909``` 910 911## connection.getNetCapabilities 912 913getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\<NetCapabilities>): void 914 915Obtains capability information of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result. 916 917**Required permission**: ohos.permission.GET_NETWORK_INFO 918 919**Atomic service API**: This API can be used in atomic services since API version 11. 920 921**System capability**: SystemCapability.Communication.NetManager.Core 922 923**Parameters** 924 925| Name | Type | Mandatory| Description | 926| --------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ | 927| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network. | 928| callback | AsyncCallback\<[NetCapabilities](#netcapabilities)> | Yes | Callback used to return the result. If the capability information of the network corresponding to the **netHandle** is obtained successfully, **error** is **undefined** and **data** is the obtained network capability information. Otherwise, **error** is an error object.| 929 930**Error codes** 931 932For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 933 934| ID| Error Message | 935| ------- | ----------------------------- | 936| 201 | Permission denied. | 937| 401 | Parameter error. | 938| 2100001 | Invalid parameter value. | 939| 2100002 | Failed to connect to the service.| 940| 2100003 | System internal error. | 941 942**Example** 943 944```ts 945import { connection } from '@kit.NetworkKit'; 946import { BusinessError } from '@kit.BasicServicesKit'; 947 948connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 949 if (netHandle.netId == 0) { 950 // If no network is connected, the obtained netid of netHandler is 0, which is abnormal. You can add specific processing based on the service requirements. 951 return; 952 } 953 connection.getNetCapabilities(netHandle, (error: BusinessError, data: connection.NetCapabilities) => { 954 if (error) { 955 console.error(`Failed to get net capabilities. Code:${error.code}, message:${error.message}`); 956 return; 957 } 958 console.info("Succeeded to get data: " + JSON.stringify(data)); 959 }) 960}).catch((error: BusinessError) => { 961 console.error(JSON.stringify(error)); 962}); 963``` 964 965## connection.getNetCapabilities 966 967getNetCapabilities(netHandle: NetHandle): Promise\<NetCapabilities> 968 969Obtains capability information of the network corresponding to the **netHandle**. This API uses a promise to return the result. 970 971**Required permission**: ohos.permission.GET_NETWORK_INFO 972 973**Atomic service API**: This API can be used in atomic services since API version 11. 974 975**System capability**: SystemCapability.Communication.NetManager.Core 976 977**Parameters** 978 979| Name | Type | Mandatory| Description | 980| --------- | ----------------------- | ---- | ---------------- | 981| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 982 983**Return value** 984 985| Type | Description | 986| --------------------------------------------- | --------------------------------- | 987| Promise\<[NetCapabilities](#netcapabilities)> | Promise used to return the result.| 988 989**Error codes** 990 991For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 992 993| ID| Error Message | 994| ------- | ----------------------------- | 995| 201 | Permission denied. | 996| 401 | Parameter error. | 997| 2100001 | Invalid parameter value. | 998| 2100002 | Failed to connect to the service.| 999| 2100003 | System internal error. | 1000 1001**Example** 1002 1003```ts 1004import { connection } from '@kit.NetworkKit'; 1005 1006connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1007 if (netHandle.netId == 0) { 1008 // If no network is connected, the obtained netid of netHandler is 0, which is abnormal. You can add specific processing based on the service requirements. 1009 return; 1010 } 1011 connection.getNetCapabilities(netHandle).then((data: connection.NetCapabilities) => { 1012 console.info("Succeeded to get data: " + JSON.stringify(data)); 1013 }) 1014}).catch((error: BusinessError) => { 1015 console.error(JSON.stringify(error)); 1016}); 1017``` 1018 1019## connection.getNetCapabilitiesSync<sup>10+</sup> 1020 1021getNetCapabilitiesSync(netHandle: NetHandle): NetCapabilities 1022 1023Obtains capability information of the network corresponding to the **netHandle**. This API returns the result synchronously. 1024 1025**Required permission**: ohos.permission.GET_NETWORK_INFO 1026 1027**Atomic service API**: This API can be used in atomic services since API version 11. 1028 1029**System capability**: SystemCapability.Communication.NetManager.Core 1030 1031**Parameters** 1032 1033| Name | Type | Mandatory| Description | 1034| --------- | ----------------------- | ---- | ---------------- | 1035| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 1036 1037**Return value** 1038 1039| Type | Description | 1040| --------------------------------------------- | --------------------------------- | 1041| [NetCapabilities](#netcapabilities) | Network capability information.| 1042 1043**Error codes** 1044 1045For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1046 1047| ID| Error Message | 1048| ------- | ----------------------------- | 1049| 201 | Permission denied. | 1050| 401 | Parameter error. | 1051| 2100001 | Invalid parameter value. | 1052| 2100002 | Failed to connect to the service.| 1053| 2100003 | System internal error. | 1054 1055**Example** 1056 1057```ts 1058import { connection } from '@kit.NetworkKit'; 1059import { BusinessError } from '@kit.BasicServicesKit'; 1060 1061let netHandle: connection.NetHandle; 1062let getNetCapabilitiesSync: connection.NetCapabilities; 1063 1064connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1065 if (netHandle.netId == 0) { 1066 // If no network is connected, the obtained netid of netHandler is 0, which is abnormal. You can add specific processing based on the service requirements. 1067 return; 1068 } 1069 1070 getNetCapabilitiesSync = connection.getNetCapabilitiesSync(netHandle); 1071 console.info("Succeeded to get net capabilities sync: " + JSON.stringify(getNetCapabilitiesSync)); 1072}); 1073 1074``` 1075 1076## connection.isDefaultNetMetered<sup>9+</sup> 1077 1078isDefaultNetMetered(callback: AsyncCallback\<boolean>): void 1079 1080Checks whether the data traffic usage on the current network is metered. This API uses an asynchronous callback to return the result. 1081 1082**Required permission**: ohos.permission.GET_NETWORK_INFO 1083 1084**System capability**: SystemCapability.Communication.NetManager.Core 1085 1086**Parameters** 1087 1088| Name | Type | Mandatory| Description | 1089| -------- | ----------------------- | ---- | -------------------------------------- | 1090| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** indicates the data traffic usage is metered.| 1091 1092**Error codes** 1093 1094For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1095 1096| ID| Error Message | 1097| ------- | ----------------------------- | 1098| 201 | Permission denied. | 1099| 401 | Parameter error. | 1100| 2100002 | Failed to connect to the service.| 1101| 2100003 | System internal error. | 1102 1103**Example** 1104 1105```ts 1106import { connection } from '@kit.NetworkKit'; 1107import { BusinessError } from '@kit.BasicServicesKit'; 1108 1109connection.isDefaultNetMetered((error: BusinessError, data: boolean) => { 1110 console.log(JSON.stringify(error)); 1111 console.log('data: ' + data); 1112}); 1113``` 1114 1115## connection.isDefaultNetMetered<sup>9+</sup> 1116 1117isDefaultNetMetered(): Promise\<boolean> 1118 1119Checks whether the data traffic usage on the current network is metered. This API uses a promise to return the result. 1120 1121**Required permission**: ohos.permission.GET_NETWORK_INFO 1122 1123**System capability**: SystemCapability.Communication.NetManager.Core 1124 1125**Return value** 1126 1127| Type | Description | 1128| ----------------- | ----------------------------------------------- | 1129| Promise\<boolean> | Promise used to return the result. The value **true** indicates the data traffic usage is metered.| 1130 1131**Error codes** 1132 1133For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1134 1135| ID| Error Message | 1136| ------- | -------------------------------- | 1137| 201 | Permission denied. | 1138| 2100002 | Failed to connect to the service.| 1139| 2100003 | System internal error. | 1140 1141**Example** 1142 1143```ts 1144import { connection } from '@kit.NetworkKit'; 1145 1146connection.isDefaultNetMetered().then((data: boolean) => { 1147 console.log('data: ' + data); 1148}); 1149``` 1150 1151## connection.isDefaultNetMeteredSync<sup>10+</sup> 1152 1153isDefaultNetMeteredSync(): boolean 1154 1155Checks whether the data traffic usage on the current network is metered. This API returns the result synchronously. 1156 1157**Required permission**: ohos.permission.GET_NETWORK_INFO 1158 1159**System capability**: SystemCapability.Communication.NetManager.Core 1160 1161**Return value** 1162 1163| Type | Description | 1164| ----------------- | ----------------------------------------------- | 1165| boolean | The value **true** indicates the data traffic usage is metered.| 1166 1167**Error codes** 1168 1169For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1170 1171| ID| Error Message | 1172| ------- | -------------------------------- | 1173| 201 | Permission denied. | 1174| 2100002 | Failed to connect to the service.| 1175| 2100003 | System internal error. | 1176 1177**Example** 1178 1179```ts 1180import { connection } from '@kit.NetworkKit'; 1181 1182let isMetered = connection.isDefaultNetMeteredSync(); 1183``` 1184 1185## connection.hasDefaultNet 1186 1187hasDefaultNet(callback: AsyncCallback\<boolean>): void 1188 1189Checks whether the default data network is activated. This API uses an asynchronous callback to return the result. You can use [getDefaultNet](#connectiongetdefaultnet) to obtain the default data network, if any. 1190 1191**Required permission**: ohos.permission.GET_NETWORK_INFO 1192 1193**System capability**: SystemCapability.Communication.NetManager.Core 1194 1195**Parameters** 1196 1197| Name | Type | Mandatory| Description | 1198| -------- | ----------------------- | ---- | -------------------------------------- | 1199| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** indicates that the default data network is activated.| 1200 1201**Error codes** 1202 1203For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1204 1205| ID| Error Message | 1206| ------- | --------------------------------- | 1207| 201 | Permission denied. | 1208| 401 | Parameter error. | 1209| 2100002 | Failed to connect to the service. | 1210| 2100003 | System internal error. | 1211 1212**Example** 1213 1214```ts 1215import { connection } from '@kit.NetworkKit'; 1216import { BusinessError } from '@kit.BasicServicesKit'; 1217 1218connection.hasDefaultNet((error: BusinessError, data: boolean) => { 1219 console.log(JSON.stringify(error)); 1220 console.log('data: ' + data); 1221}); 1222``` 1223 1224## connection.hasDefaultNet 1225 1226hasDefaultNet(): Promise\<boolean> 1227 1228Checks whether the default data network is activated. This API uses a promise to return the result. You can use [getDefaultNet](#connectiongetdefaultnet) to obtain the default data network, if any. 1229 1230**Required permission**: ohos.permission.GET_NETWORK_INFO 1231 1232**System capability**: SystemCapability.Communication.NetManager.Core 1233 1234**Return value** 1235 1236| Type | Description | 1237| ----------------- | ----------------------------------------------- | 1238| Promise\<boolean> | Promise used to return the result. The value **true** indicates that the default data network is activated.| 1239 1240**Error codes** 1241 1242For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1243 1244| ID| Error Message | 1245| ------- | ----------------------------- | 1246| 201 | Permission denied. | 1247| 2100002 | Failed to connect to the service. | 1248| 2100003 | System internal error. | 1249 1250**Example** 1251 1252```ts 1253import { connection } from '@kit.NetworkKit'; 1254 1255connection.hasDefaultNet().then((data: boolean) => { 1256 console.log('data: ' + data); 1257}); 1258``` 1259 1260## connection.hasDefaultNetSync<sup>10+</sup> 1261 1262hasDefaultNetSync(): boolean 1263 1264Checks whether the default data network is activated. This API returns the result synchronously. 1265 1266**Required permission**: ohos.permission.GET_NETWORK_INFO 1267 1268**System capability**: SystemCapability.Communication.NetManager.Core 1269 1270**Return value** 1271 1272| Type | Description | 1273| ----------------- | ----------------------------------------------- | 1274| boolean | The value **true** indicates that the default data network is activated.| 1275 1276**Error codes** 1277 1278For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1279 1280| ID| Error Message | 1281| ------- | ----------------------------- | 1282| 201 | Permission denied. | 1283| 2100002 | Failed to connect to the service.| 1284| 2100003 | System internal error. | 1285 1286**Example** 1287 1288```ts 1289import { connection } from '@kit.NetworkKit'; 1290 1291let isDefaultNet = connection.hasDefaultNetSync(); 1292``` 1293 1294 1295## connection.reportNetConnected 1296 1297reportNetConnected(netHandle: NetHandle, callback: AsyncCallback<void>): void 1298 1299Reports connection of the data network to the network management module. This API uses an asynchronous callback to return the result. 1300 1301**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 1302 1303**System capability**: SystemCapability.Communication.NetManager.Core 1304 1305**Parameters** 1306 1307| Name| Type| Mandatory| Description| 1308| -------- | -------- | -------- | -------- | 1309| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 1310| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the network status is reported successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1311 1312**Error codes** 1313 1314For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1315 1316| ID| Error Message | 1317| ------- | ----------------------------- | 1318| 201 | Permission denied. | 1319| 401 | Parameter error. | 1320| 2100001 | Invalid parameter value. | 1321| 2100002 | Failed to connect to the service. | 1322| 2100003 | System internal error. | 1323 1324**Example** 1325 1326```ts 1327import { connection } from '@kit.NetworkKit'; 1328import { BusinessError } from '@kit.BasicServicesKit'; 1329 1330connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1331 connection.reportNetConnected(netHandle, (error: BusinessError) => { 1332 console.log(JSON.stringify(error)); 1333 }); 1334}); 1335``` 1336 1337## connection.reportNetConnected 1338 1339reportNetConnected(netHandle: NetHandle): Promise\<void\> 1340 1341Reports connection of the data network to the network management module. This API uses a promise to return the result. 1342 1343**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 1344 1345**System capability**: SystemCapability.Communication.NetManager.Core 1346 1347**Parameters** 1348 1349| Name| Type| Mandatory| Description| 1350| -------- | -------- | -------- | -------- | 1351| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 1352 1353**Return value** 1354| Type| Description| 1355| -------- | -------- | 1356| Promise<void> | Promise that returns no value.| 1357 1358**Error codes** 1359 1360For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1361 1362| ID| Error Message | 1363| ------- | --------------------------------- | 1364| 201 | Permission denied. | 1365| 401 | Parameter error. | 1366| 2100001 | Invalid parameter value. | 1367| 2100002 | Failed to connect to the service. | 1368| 2100003 | System internal error. | 1369 1370**Example** 1371 1372```ts 1373import { connection } from '@kit.NetworkKit'; 1374 1375connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1376 connection.reportNetConnected(netHandle).then(() => { 1377 console.log(`report success`); 1378 }); 1379}); 1380``` 1381 1382## connection.reportNetDisconnected 1383 1384reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback<void>): void 1385 1386Reports disconnection of the data network to the network management module. This API uses an asynchronous callback to return the result. 1387 1388**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 1389 1390**System capability**: SystemCapability.Communication.NetManager.Core 1391 1392**Parameters** 1393 1394| Name| Type| Mandatory| Description| 1395| -------- | -------- | -------- | -------- | 1396| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 1397| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the network status is reported successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1398 1399**Error codes** 1400 1401For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1402 1403| ID| Error Message | 1404| ------- | ----------------------------- | 1405| 201 | Permission denied. | 1406| 401 | Parameter error. | 1407| 2100001 | Invalid parameter value. | 1408| 2100002 | Failed to connect to the service. | 1409| 2100003 | System internal error. | 1410 1411**Example** 1412 1413```ts 1414import { connection } from '@kit.NetworkKit'; 1415 1416connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1417 connection.reportNetDisconnected(netHandle).then( () => { 1418 console.log(`report success`); 1419 }); 1420}); 1421``` 1422 1423## connection.reportNetDisconnected 1424 1425reportNetDisconnected(netHandle: NetHandle): Promise<void> 1426 1427Reports disconnection of the data network to the network management module. This API uses a promise to return the result. 1428 1429**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 1430 1431**System capability**: SystemCapability.Communication.NetManager.Core 1432 1433**Parameters** 1434 1435| Name| Type| Mandatory| Description| 1436| -------- | -------- | -------- | -------- | 1437| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 1438 1439**Return value** 1440| Type| Description| 1441| -------- | -------- | 1442| Promise<void> | Promise that returns no value.| 1443 1444**Error codes** 1445 1446For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1447 1448| ID| Error Message | 1449| ------- | --------------------------------- | 1450| 201 | Permission denied. | 1451| 401 | Parameter error. | 1452| 2100001 | Invalid parameter value. | 1453| 2100002 | Failed to connect to the service. | 1454| 2100003 | System internal error. | 1455 1456**Example** 1457 1458```ts 1459import { connection } from '@kit.NetworkKit'; 1460 1461connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1462 connection.reportNetDisconnected(netHandle).then( () => { 1463 console.log(`report success`); 1464 }); 1465}); 1466``` 1467 1468## connection.getAddressesByName 1469 1470getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void 1471 1472Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses an asynchronous callback to return the result. 1473 1474**Required permissions**: ohos.permission.INTERNET 1475 1476**System capability**: SystemCapability.Communication.NetManager.Core 1477 1478**Parameters** 1479 1480| Name | Type | Mandatory| Description | 1481| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 1482| host | string | Yes | Host name to resolve. | 1483| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes | Callback used to return the result. If all IP addresses are successfully obtained, **error** is **undefined**, and **data** is the list of all obtained IP addresses. Otherwise, **error** is an error object.| 1484 1485**Error codes** 1486 1487For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1488 1489| ID| Error Message | 1490| ------- | --------------------------------- | 1491| 201 | Permission denied. | 1492| 401 | Parameter error. | 1493| 2100001 | Invalid parameter value. | 1494| 2100002 | Failed to connect to the service. | 1495| 2100003 | System internal error. | 1496 1497**Example** 1498 1499```ts 1500import { connection } from '@kit.NetworkKit'; 1501import { BusinessError } from '@kit.BasicServicesKit'; 1502 1503connection.getAddressesByName("xxxx", (error: BusinessError, data: connection.NetAddress[]) => { 1504 if (error) { 1505 console.error(`Failed to get addresses. Code:${error.code}, message:${error.message}`); 1506 return; 1507 } 1508 console.info("Succeeded to get data: " + JSON.stringify(data)); 1509}); 1510``` 1511 1512## connection.getAddressesByName 1513 1514getAddressesByName(host: string): Promise\<Array\<NetAddress\>\> 1515 1516Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses a promise to return the result. 1517 1518**Required permissions**: ohos.permission.INTERNET 1519 1520**System capability**: SystemCapability.Communication.NetManager.Core 1521 1522**Parameters** 1523 1524| Name| Type | Mandatory| Description | 1525| ------ | ------ | ---- | ------------------ | 1526| host | string | Yes | Host name to resolve.| 1527 1528**Return value** 1529 1530| Type | Description | 1531| ------------------------------------------- | ----------------------------- | 1532| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.| 1533 1534**Error codes** 1535 1536For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1537 1538| ID| Error Message | 1539| ------- | ----------------------------- | 1540| 201 | Permission denied. | 1541| 401 | Parameter error. | 1542| 2100001 | Invalid parameter value. | 1543| 2100002 | Failed to connect to the service. | 1544| 2100003 | System internal error. | 1545 1546**Example** 1547 1548```ts 1549import { connection } from '@kit.NetworkKit'; 1550 1551connection.getAddressesByName("xxxx").then((data: connection.NetAddress[]) => { 1552 console.info("Succeeded to get data: " + JSON.stringify(data)); 1553}); 1554``` 1555 1556## connection.addCustomDnsRule<sup>11+</sup> 1557 1558addCustomDnsRule(host: string, ip: Array\<string\>, callback: AsyncCallback\<void\>): void 1559 1560Adds custom DNS rules for the specified host of the current application. This API uses an asynchronous callback to return the result. 1561 1562**Required permissions**: ohos.permission.INTERNET 1563 1564**System capability**: SystemCapability.Communication.NetManager.Core 1565 1566**Parameters** 1567 1568| Name | Type | Mandatory| Description | 1569| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1570| host | string | Yes | Name of the custom host. | 1571| ip | Array\<string> | Yes | List of IP addresses mapped to the host name. | 1572| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the mapping is added successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1573 1574**Error codes** 1575 1576For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1577 1578| ID| Error Message | 1579| ------- | --------------------------------- | 1580| 201 | Permission denied. | 1581| 401 | Parameter error. | 1582| 2100001 | Invalid parameter value. | 1583| 2100002 | Failed to connect to the service. | 1584| 2100003 | System internal error. | 1585 1586**Example** 1587 1588```ts 1589import { connection } from '@kit.NetworkKit'; 1590import { BusinessError } from '@kit.BasicServicesKit'; 1591 1592connection.addCustomDnsRule("xxxx", ["xx.xx.xx.xx","xx.xx.xx.xx"], (error: BusinessError, data: void) => { 1593 if (error) { 1594 console.error(`Failed to get add custom dns rule. Code:${error.code}, message:${error.message}`); 1595 return; 1596 } 1597 console.info("Succeeded to get data: " + JSON.stringify(data)); 1598}) 1599``` 1600 1601## connection.addCustomDnsRule<sup>11+</sup> 1602 1603addCustomDnsRule(host: string, ip: Array\<string\>): Promise\<void\> 1604 1605Adds custom DNS rules for the specified host of the current application. This API uses a promise to return the result. 1606 1607**Required permissions**: ohos.permission.INTERNET 1608 1609**System capability**: SystemCapability.Communication.NetManager.Core 1610 1611**Parameters** 1612 1613| Name| Type | Mandatory| Description | 1614| ------ | -------------- | ---- | -------------------------- | 1615| host | string | Yes | Name of the custom host. | 1616| ip | Array\<string> | Yes | List of IP addresses mapped to the host name.| 1617 1618**Return value** 1619 1620| Type | Description | 1621| ---------------------- | ----------------------- | 1622| Promise\<Array\<void>> | Promise that returns no value.| 1623 1624**Error codes** 1625 1626For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1627 1628| ID| Error Message | 1629| ------- | --------------------------------- | 1630| 201 | Permission denied. | 1631| 401 | Parameter error. | 1632| 2100001 | Invalid parameter value. | 1633| 2100002 | Failed to connect to the service. | 1634| 2100003 | System internal error. | 1635 1636**Example** 1637 1638```ts 1639import { connection } from '@kit.NetworkKit'; 1640import { BusinessError } from '@kit.BasicServicesKit'; 1641 1642connection.addCustomDnsRule("xxxx", ["xx.xx.xx.xx","xx.xx.xx.xx"]).then(() => { 1643 console.info("success"); 1644}).catch((error: BusinessError) => { 1645 console.error(JSON.stringify(error)); 1646}) 1647``` 1648 1649## connection.removeCustomDnsRule<sup>11+</sup> 1650 1651removeCustomDnsRule(host: string, callback: AsyncCallback\<void\>): void 1652 1653Removes the custom DNS rules of the specified host from the current application. This API uses an asynchronous callback to return the result. 1654 1655**Required permissions**: ohos.permission.INTERNET 1656 1657**System capability**: SystemCapability.Communication.NetManager.Core 1658 1659**Parameters** 1660 1661| Name | Type | Mandatory| Description | 1662| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1663| host | string | Yes | Name of the host for which DNS rules are to be deleted. | 1664| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the DNS rules are removed successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1665 1666**Error codes** 1667 1668For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1669 1670| ID| Error Message | 1671| ------- | ----------------------------- | 1672| 201 | Permission denied. | 1673| 401 | Parameter error. | 1674| 2100001 | Invalid parameter value. | 1675| 2100002 | Failed to connect to the service. | 1676| 2100003 | System internal error. | 1677 1678**Example** 1679 1680```ts 1681import { connection } from '@kit.NetworkKit'; 1682import { BusinessError } from '@kit.BasicServicesKit'; 1683 1684connection.removeCustomDnsRule("xxxx", (error: BusinessError, data: void) => { 1685 if (error) { 1686 console.error(`Failed to remove custom dns rule. Code:${error.code}, message:${error.message}`); 1687 return; 1688 } 1689 console.info("Succeeded to get data: " + JSON.stringify(data)); 1690}) 1691``` 1692 1693## connection.removeCustomDnsRule<sup>11+</sup> 1694 1695removeCustomDnsRule(host: string): Promise\<void\> 1696 1697Removes the custom DNS rules of the specified host from the current application. This API uses a promise to return the result. 1698 1699**Required permissions**: ohos.permission.INTERNET 1700 1701**System capability**: SystemCapability.Communication.NetManager.Core 1702 1703**Parameters** 1704 1705| Name| Type | Mandatory| Description | 1706| ------ | ------ | ---- | ------------------------------- | 1707| host | string | Yes | Name of the host for which DNS rules are to be deleted.| 1708 1709**Return value** 1710 1711| Type | Description | 1712| ---------------------- | ----------------------- | 1713| Promise\<Array\<void>> | Promise that returns no value.| 1714 1715**Error codes** 1716 1717For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1718 1719| ID| Error Message | 1720| ------- | --------------------------------- | 1721| 201 | Permission denied. | 1722| 401 | Parameter error. | 1723| 2100001 | Invalid parameter value. | 1724| 2100002 | Failed to connect to the service. | 1725| 2100003 | System internal error. | 1726 1727**Example** 1728 1729```ts 1730import { connection } from '@kit.NetworkKit'; 1731import { BusinessError } from '@kit.BasicServicesKit'; 1732 1733connection.removeCustomDnsRule("xxxx").then(() => { 1734 console.log("success"); 1735}).catch((error: BusinessError) => { 1736 console.log(JSON.stringify(error)); 1737}) 1738``` 1739 1740## connection.clearCustomDnsRules<sup>11+</sup> 1741 1742clearCustomDnsRules(callback: AsyncCallback\<void\>): void 1743 1744Removes all custom DNS rules from the current application. This API uses an asynchronous callback to return the result. 1745 1746**Required permissions**: ohos.permission.INTERNET 1747 1748**System capability**: SystemCapability.Communication.NetManager.Core 1749 1750**Parameters** 1751 1752| Name | Type | Mandatory| Description | 1753| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1754| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If all the DNS rules are removed successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1755 1756**Error codes** 1757 1758For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1759 1760| ID| Error Message | 1761| ------- | --------------------------------- | 1762| 201 | Permission denied. | 1763| 401 | Parameter error. | 1764| 2100001 | Invalid parameter value. | 1765| 2100002 | Failed to connect to the service. | 1766| 2100003 | System internal error. | 1767 1768**Example** 1769 1770```ts 1771import { connection } from '@kit.NetworkKit'; 1772import { BusinessError } from '@kit.BasicServicesKit'; 1773 1774connection.clearCustomDnsRules((error: BusinessError, data: void) => { 1775 if (error) { 1776 console.error(`Failed to clear custom dns rules. Code:${error.code}, message:${error.message}`); 1777 return; 1778 } 1779 console.info("Succeeded to get data: " + JSON.stringify(data)); 1780}) 1781``` 1782 1783## connection.clearCustomDnsRules<sup>11+</sup> 1784 1785clearCustomDnsRules(): Promise\<void\> 1786 1787Removes all custom DNS rules from the current application. This API uses a promise to return the result. 1788 1789**Required permissions**: ohos.permission.INTERNET 1790 1791**System capability**: SystemCapability.Communication.NetManager.Core 1792 1793**Return value** 1794 1795| Type | Description | 1796| ---------------------- | ----------------------- | 1797| Promise\<void\> | Promise that returns no value. | 1798 1799**Error codes** 1800 1801For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1802 1803| ID| Error Message | 1804| ------- | --------------------------------- | 1805| 201 | Permission denied. | 1806| 2100001 | Invalid parameter value. | 1807| 2100002 | Failed to connect to the service. | 1808| 2100003 | System internal error. | 1809 1810**Example** 1811 1812```ts 1813import { connection } from '@kit.NetworkKit'; 1814import { BusinessError } from '@kit.BasicServicesKit'; 1815 1816connection.clearCustomDnsRules().then(() => { 1817 console.log("success"); 1818}).catch((error: BusinessError) => { 1819 console.log(JSON.stringify(error)); 1820}) 1821``` 1822 1823 1824## NetConnection 1825 1826Represents the network connection handle. 1827 1828> **NOTE** 1829> When a device changes to the network connected state, the **netAvailable**, **netCapabilitiesChange**, and **netConnectionPropertiesChange** events will be triggered. 1830> When a device changes to the network disconnected state, the **netLost** event will be triggered. 1831> When a device switches from a Wi-Fi network to a cellular network, the **netLost** event will be first triggered to indicate that the Wi-Fi network is lost and then the **netAvailable** event will be triggered to indicate that the cellular network is available. 1832 1833### register 1834 1835register(callback: AsyncCallback\<void>): void 1836 1837Registers a listener for network status changes. 1838 1839**Required permission**: ohos.permission.GET_NETWORK_INFO 1840 1841**Atomic service API**: This API can be used in atomic services since API version 11. 1842 1843**System capability**: SystemCapability.Communication.NetManager.Core 1844 1845**Parameters** 1846 1847| Name | Type | Mandatory| Description | 1848| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1849| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If a listener for network status changes is registered successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1850 1851**Error codes** 1852 1853For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1854 1855| ID| Error Message | 1856| ------- | ---------------------------------------------------- | 1857| 201 | Permission denied. | 1858| 401 | Parameter error. | 1859| 2100002 | Failed to connect to the service. | 1860| 2100003 | System internal error. | 1861| 2101008 | The callback already exists. | 1862| 2101022 | The number of requests exceeded the maximum allowed. | 1863 1864**Example** 1865 1866```ts 1867import { connection } from '@kit.NetworkKit'; 1868import { BusinessError } from '@kit.BasicServicesKit'; 1869 1870let netCon: connection.NetConnection = connection.createNetConnection(); 1871netCon.register((error: BusinessError) => { 1872 console.log(JSON.stringify(error)); 1873}); 1874``` 1875 1876### unregister 1877 1878unregister(callback: AsyncCallback\<void>): void 1879 1880Unregisters the listener for network status changes. 1881 1882**Atomic service API**: This API can be used in atomic services since API version 11. 1883 1884**System capability**: SystemCapability.Communication.NetManager.Core 1885 1886**Parameters** 1887 1888| Name | Type | Mandatory| Description | 1889| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1890| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If a listener for network status changes is unregistered successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1891 1892**Error codes** 1893 1894For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1895 1896| ID| Error Message | 1897| ------- | --------------------------------- | 1898| 401 | Parameter error. | 1899| 2100002 | Failed to connect to the service. | 1900| 2100003 | System internal error. | 1901| 2101007 | The callback does not exist. | 1902 1903**Example** 1904 1905```ts 1906import { connection } from '@kit.NetworkKit'; 1907import { BusinessError } from '@kit.BasicServicesKit'; 1908 1909let netCon: connection.NetConnection = connection.createNetConnection(); 1910netCon.unregister((error: BusinessError) => { 1911 console.log(JSON.stringify(error)); 1912}); 1913``` 1914 1915### on('netAvailable') 1916 1917on(type: 'netAvailable', callback: Callback\<NetHandle>): void 1918 1919Registers a listener for **netAvailable** events. Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 1920 1921**Atomic service API**: This API can be used in atomic services since API version 11. 1922 1923**System capability**: SystemCapability.Communication.NetManager.Core 1924 1925**Parameters** 1926 1927| Name | Type | Mandatory| Description | 1928| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 1929| type | string | Yes | Event type. This field has a fixed value of **netAvailable**.<br>**netAvailable**: event indicating that the data network is available.| 1930| callback | Callback\<[NetHandle](#nethandle)> | Yes | Callback used to return the network handle.| 1931 1932**Example** 1933 1934```ts 1935import { connection } from '@kit.NetworkKit'; 1936import { BusinessError } from '@kit.BasicServicesKit'; 1937 1938// Create a NetConnection object. 1939let netCon: connection.NetConnection = connection.createNetConnection(); 1940 1941// Call register to register a listener. 1942netCon.register((error: BusinessError) => { 1943 console.log(JSON.stringify(error)); 1944}); 1945 1946// Subscribe to netAvailable events. Event notifications can be received only after register is called. 1947netCon.on('netAvailable', (data: connection.NetHandle) => { 1948 console.info("Succeeded to get data: " + JSON.stringify(data)); 1949}); 1950 1951// Call unregister to unregister the listener. 1952netCon.unregister((error: BusinessError) => { 1953 console.log(JSON.stringify(error)); 1954}); 1955``` 1956 1957### on('netBlockStatusChange') 1958 1959on(type: 'netBlockStatusChange', callback: Callback\<NetBlockStatusInfo>): void 1960 1961Registers a listener for **netBlockStatusChange** events. Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 1962 1963**System capability**: SystemCapability.Communication.NetManager.Core 1964 1965**Parameters** 1966 1967| Name | Type | Mandatory| Description | 1968| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1969| type | string | Yes | Event type. This field has a fixed value of **netBlockStatusChange**.<br>**netBlockStatusChange**: event indicating a change in the network blocking status.| 1970| callback | Callback<[NetBlockStatusInfo](#netblockstatusinfo11)> | Yes | Callback used to return the result.| 1971 1972**Example** 1973 1974```ts 1975import { connection } from '@kit.NetworkKit'; 1976import { BusinessError } from '@kit.BasicServicesKit'; 1977 1978// Create a NetConnection object. 1979let netCon: connection.NetConnection = connection.createNetConnection(); 1980 1981// Call register to register a listener. 1982netCon.register((error: BusinessError) => { 1983 console.log(JSON.stringify(error)); 1984}); 1985 1986// Subscribe to netBlockStatusChange events. Event notifications can be received only after register is called. 1987netCon.on('netBlockStatusChange', (data: connection.NetBlockStatusInfo) => { 1988 console.info("Succeeded to get data: " + JSON.stringify(data)); 1989}); 1990 1991// Call unregister to unregister the listener. 1992netCon.unregister((error: BusinessError) => { 1993 console.log(JSON.stringify(error)); 1994}); 1995``` 1996 1997### on('netCapabilitiesChange') 1998 1999on(type: 'netCapabilitiesChange', callback: Callback\<NetCapabilityInfo\>): void 2000 2001Registers a listener for **netCapabilitiesChange** events. Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 2002 2003**Atomic service API**: This API can be used in atomic services since API version 11. 2004 2005**System capability**: SystemCapability.Communication.NetManager.Core 2006 2007**Parameters** 2008 2009| Name | Type | Mandatory| Description | 2010| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2011| type | string | Yes | Event type. This field has a fixed value of **netCapabilitiesChange**.<br>**netCapabilitiesChange**: event indicating that the network capabilities have changed.| 2012| callback | Callback<[NetCapabilityInfo](#netcapabilityinfo10)> | Yes | Callback used to return the network handle (**netHandle**) and capability information (**netCap**).| 2013 2014**Example** 2015 2016```ts 2017import { connection } from '@kit.NetworkKit'; 2018import { BusinessError } from '@kit.BasicServicesKit'; 2019 2020// Create a NetConnection object. 2021let netCon: connection.NetConnection = connection.createNetConnection(); 2022 2023// Call register to register a listener. 2024netCon.register((error: BusinessError) => { 2025 console.log(JSON.stringify(error)); 2026}); 2027 2028// Subscribe to netCapabilitiesChange events. Event notifications can be received only after register is called. 2029netCon.on('netCapabilitiesChange', (data: connection.NetCapabilityInfo) => { 2030 console.info("Succeeded to get data: " + JSON.stringify(data)); 2031}); 2032 2033// Call unregister to unregister the listener. 2034netCon.unregister((error: BusinessError) => { 2035 console.log(JSON.stringify(error)); 2036}); 2037``` 2038 2039### on('netConnectionPropertiesChange') 2040 2041on(type: 'netConnectionPropertiesChange', callback: Callback\<NetConnectionPropertyInfo\>): void 2042 2043Registers a listener for **netConnectionPropertiesChange** events. Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 2044 2045**System capability**: SystemCapability.Communication.NetManager.Core 2046 2047**Parameters** 2048 2049| Name | Type | Mandatory| Description | 2050| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2051| type | string | Yes | Event type. This field has a fixed value of **netConnectionPropertiesChange**.<br>**netConnectionPropertiesChange**: event indicating that network connection properties have changed.| 2052| callback | Callback<[NetConnectionPropertyInfo](#netconnectionpropertyinfo11)> | Yes | Callback used to return the result.| 2053 2054**Example** 2055 2056```ts 2057import { connection } from '@kit.NetworkKit'; 2058import { BusinessError } from '@kit.BasicServicesKit'; 2059 2060// Create a NetConnection object. 2061let netCon: connection.NetConnection = connection.createNetConnection(); 2062 2063// Call register to register a listener. 2064netCon.register((error: BusinessError) => { 2065 console.log(JSON.stringify(error)); 2066}); 2067 2068// Subscribe to netConnectionPropertiesChange events. Event notifications can be received only after register is called. 2069netCon.on('netConnectionPropertiesChange', (data: connection.NetConnectionPropertyInfo) => { 2070 console.info("Succeeded to get data: " + JSON.stringify(data)); 2071}); 2072 2073// Call unregister to unregister the listener. 2074netCon.unregister((error: BusinessError) => { 2075 console.log(JSON.stringify(error)); 2076}); 2077``` 2078 2079### on('netLost') 2080 2081on(type: 'netLost', callback: Callback\<NetHandle>): void 2082 2083Registers a listener for **netLost** events. Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 2084 2085**Atomic service API**: This API can be used in atomic services since API version 11. 2086 2087**System capability**: SystemCapability.Communication.NetManager.Core 2088 2089**Parameters** 2090 2091| Name | Type | Mandatory| Description | 2092| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 2093| type | string | Yes | Event type. This field has a fixed value of **netLost**.<br>netLost: event indicating that the network is interrupted or normally disconnected.| 2094| callback | Callback\<[NetHandle](#nethandle)> | Yes | Callback used to return **netHandle**.| 2095 2096**Example** 2097 2098```ts 2099import { connection } from '@kit.NetworkKit'; 2100import { BusinessError } from '@kit.BasicServicesKit'; 2101 2102// Create a NetConnection object. 2103let netCon: connection.NetConnection = connection.createNetConnection(); 2104 2105// Call register to register a listener. 2106netCon.register((error: BusinessError) => { 2107 console.log(JSON.stringify(error)); 2108}); 2109 2110// Subscribe to netLost events. Event notifications can be received only after register is called. 2111netCon.on('netLost', (data: connection.NetHandle) => { 2112 console.info("Succeeded to get data: " + JSON.stringify(data)); 2113}); 2114 2115// Call unregister to unregister the listener. 2116netCon.unregister((error: BusinessError) => { 2117 console.log(JSON.stringify(error)); 2118}); 2119``` 2120 2121### on('netUnavailable') 2122 2123on(type: 'netUnavailable', callback: Callback\<void>): void 2124 2125Registers a listener for **netUnavailable** events. Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 2126 2127**Atomic service API**: This API can be used in atomic services since API version 11. 2128 2129**System capability**: SystemCapability.Communication.NetManager.Core 2130 2131**Parameters** 2132 2133| Name | Type | Mandatory| Description | 2134| -------- | --------------- | ---- | ------------------------------------------------------------ | 2135| type | string | Yes | Event type. This field has a fixed value of **netUnavailable**.<br>**netUnavailable**: event indicating that the network is unavailable.| 2136| callback | Callback\<void> | Yes | Callback used to return the result, which is empty.| 2137 2138**Example** 2139 2140```ts 2141import { connection } from '@kit.NetworkKit'; 2142import { BusinessError } from '@kit.BasicServicesKit'; 2143 2144// Create a NetConnection object. 2145let netCon: connection.NetConnection = connection.createNetConnection(); 2146 2147// Call register to register a listener. 2148netCon.register((error: BusinessError) => { 2149 console.log(JSON.stringify(error)); 2150}); 2151 2152// Subscribe to netUnavailable events. Event notifications can be received only after register is called. 2153netCon.on('netUnavailable', () => { 2154 console.info("Succeeded to get unavailable net event"); 2155}); 2156 2157// Call unregister to unregister the listener. 2158netCon.unregister((error: BusinessError) => { 2159 console.log(JSON.stringify(error)); 2160}); 2161``` 2162 2163## NetHandle 2164 2165Defines the handle of the data network. 2166 2167Before invoking **NetHandle** APIs, call **getNetHandle** to obtain a **NetHandle** object. 2168 2169**System capability**: SystemCapability.Communication.NetManager.Core 2170 2171### Attributes 2172 2173| Name | Type | Mandatory| Description | 2174| ------ | ------ | --- |------------------------- | 2175| netId | number | Yes | Network ID. The value **0** indicates no default network. Any other value must be greater than or equal to 100.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 2176 2177### bindSocket<sup>9+</sup> 2178 2179bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void 2180 2181Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses an asynchronous callback to return the result. 2182 2183**System capability**: SystemCapability.Communication.NetManager.Core 2184 2185**Parameters** 2186 2187| Name | Type | Mandatory| Description | 2188| ----------- | ------------------------ | ---- | -------------------------------| 2189| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes| **TCPSocket** or **UDPSocket** object.| 2190| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the **TCPSocket** or **UDPSocket** object is successfully bound to the current network, **error** is **undefined**. Otherwise, **error** is an error object.| 2191 2192**Error codes** 2193 2194For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 2195 2196| ID| Error Message | 2197| ------- | --------------------------------- | 2198| 401 | Parameter error. | 2199| 2100001 | Invalid parameter value. | 2200| 2100002 | Failed to connect to the service. | 2201| 2100003 | System internal error. | 2202 2203**Example** 2204 2205```ts 2206import { connection, socket } from '@kit.NetworkKit'; 2207import { BusinessError } from '@kit.BasicServicesKit'; 2208 2209interface Data { 2210 message: ArrayBuffer, 2211 remoteInfo: socket.SocketRemoteInfo 2212} 2213 2214 connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2215 if (netHandle.netId == 0) { 2216 // If no network is connected, the obtained netid of netHandler is 0, which is abnormal. You can add specific processing based on the service requirements. 2217 } 2218 let tcp : socket.TCPSocket = socket.constructTCPSocketInstance(); 2219 let udp : socket.UDPSocket = socket.constructUDPSocketInstance(); 2220 let socketType = "TCPSocket"; 2221 if (socketType == "TCPSocket") { 2222 tcp.bind({address:"192.168.xxx.xxx", 2223 port:8080, 2224 family:1} as socket.NetAddress, (error: Error) => { 2225 if (error) { 2226 console.log('bind fail'); 2227 return; 2228 } 2229 netHandle.bindSocket(tcp, (error: BusinessError, data: void) => { 2230 if (error) { 2231 console.error(`Failed to bind socket. Code:${error.code}, message:${error.message}`); 2232 return; 2233 } else { 2234 console.info(JSON.stringify(data)); 2235 } 2236 }); 2237 }); 2238 } else { 2239 let callback: (value: Data) => void = (value: Data) => { 2240 console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); 2241 }; 2242 udp.bind({address:"192.168.xxx.xxx", 2243 port:8080, 2244 family:1} as socket.NetAddress, (error: BusinessError) => { 2245 if (error) { 2246 console.error(`Failed to bind. Code:${error.code}, message:${error.message}`); 2247 return; 2248 } 2249 udp.on('message', (data: Data) => { 2250 console.info("Succeeded to get data: " + JSON.stringify(data)); 2251 }); 2252 netHandle.bindSocket(udp, (error: BusinessError, data: void) => { 2253 if (error) { 2254 console.error(`Failed to bind socket. Code:${error.code}, message:${error.message}`); 2255 return; 2256 } else { 2257 console.info(JSON.stringify(data)); 2258 } 2259 }); 2260 }); 2261 } 2262}) 2263``` 2264 2265### bindSocket<sup>9+</sup> 2266 2267bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void\> 2268 2269Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses a promise to return the result. 2270 2271**System capability**: SystemCapability.Communication.NetManager.Core 2272 2273**Parameters** 2274 2275| Name | Type | Mandatory | Description | 2276| --------------- | --------------------- | ---- | ------------------------------ | 2277| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes | **TCPSocket** or **UDPSocket** object.| 2278 2279**Return value** 2280 2281| Type | Description | 2282| -------------- | ---------------------- | 2283| Promise\<void> | Promise that returns no value.| 2284 2285**Error codes** 2286 2287For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 2288 2289| ID| Error Message | 2290| ------- | --------------------------------- | 2291| 401 | Parameter error. | 2292| 2100001 | Invalid parameter value. | 2293| 2100002 | Failed to connect to the service. | 2294| 2100003 | System internal error. | 2295 2296**Example** 2297 2298```ts 2299import { connection, socket } from '@kit.NetworkKit'; 2300import { BusinessError } from '@kit.BasicServicesKit'; 2301 2302interface Data { 2303 message: ArrayBuffer, 2304 remoteInfo: socket.SocketRemoteInfo 2305} 2306 2307connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2308 if (netHandle.netId == 0) { 2309 // If no network is connected, the obtained netid of netHandler is 0, which is abnormal. You can add specific processing based on the service requirements. 2310 return; 2311 } 2312 let tcp : socket.TCPSocket = socket.constructTCPSocketInstance(); 2313 let udp : socket.UDPSocket = socket.constructUDPSocketInstance(); 2314 let socketType = "TCPSocket"; 2315 if (socketType == "TCPSocket") { 2316 tcp.bind({address:"192.168.xxx.xxx", 2317 port:8080, 2318 family:1} as socket.NetAddress, (error: Error) => { 2319 if (error) { 2320 console.log('bind fail'); 2321 return; 2322 } 2323 netHandle.bindSocket(tcp).then(() => { 2324 console.info("bind socket success"); 2325 }).catch((error: BusinessError) => { 2326 console.error(`Failed to bind socket. Code:${error.code}, message:${error.message}`); 2327 }); 2328 }); 2329 } else { 2330 let callback: (value: Data) => void = (value: Data) => { 2331 console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); 2332 } 2333 udp.bind({address:"192.168.xxx.xxx", 2334 port:8080, 2335 family:1} as socket.NetAddress, (error: BusinessError) => { 2336 if (error) { 2337 console.error(`Failed to bind. Code:${error.code}, message:${error.message}`); 2338 return; 2339 } 2340 udp.on('message', (data: Data) => { 2341 console.info("Succeeded to get data: " + JSON.stringify(data)); 2342 }); 2343 netHandle.bindSocket(udp).then(() => { 2344 console.info("bind socket success"); 2345 }).catch((error: BusinessError) => { 2346 console.error(`Failed to bind socket. Code:${error.code}, message:${error.message}`); 2347 }); 2348 }); 2349 } 2350}); 2351``` 2352 2353### getAddressesByName 2354 2355getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>\>\): void 2356 2357Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses an asynchronous callback to return the result. 2358 2359**Required permissions**: ohos.permission.INTERNET 2360 2361**System capability**: SystemCapability.Communication.NetManager.Core 2362 2363**Parameters** 2364 2365| Name | Type | Mandatory| Description | 2366| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 2367| host | string | Yes | Host name to resolve. | 2368| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes | Callback used to return the result. If all IP addresses are successfully obtained, **error** is **undefined**, and **data** is the list of all obtained IP addresses. Otherwise, **error** is an error object.| 2369 2370**Error codes** 2371 2372For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 2373 2374| ID| Error Message | 2375| ------- | --------------------------------- | 2376| 201 | Permission denied. | 2377| 401 | Parameter error. | 2378| 2100001 | Invalid parameter value. | 2379| 2100002 | Failed to connect to the service. | 2380| 2100003 | System internal error. | 2381 2382**Example** 2383 2384```ts 2385import { connection } from '@kit.NetworkKit'; 2386import { BusinessError } from '@kit.BasicServicesKit'; 2387 2388connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2389 if (netHandle.netId == 0) { 2390 // If no network is connected, the obtained netid of netHandler is 0, which is abnormal. You can add specific processing based on the service requirements. 2391 return; 2392 } 2393 let host = "xxxx"; 2394 netHandle.getAddressesByName(host, (error: BusinessError, data: connection.NetAddress[]) => { 2395 if (error) { 2396 console.error(`Failed to get addresses. Code:${error.code}, message:${error.message}`); 2397 return; 2398 } 2399 console.info("Succeeded to get data: " + JSON.stringify(data)); 2400 }); 2401}); 2402``` 2403 2404### getAddressesByName 2405 2406getAddressesByName(host: string): Promise\<Array\<NetAddress>> 2407 2408Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses a promise to return the result. 2409 2410**Required permissions**: ohos.permission.INTERNET 2411 2412**System capability**: SystemCapability.Communication.NetManager.Core 2413 2414**Parameters** 2415 2416| Name| Type | Mandatory| Description | 2417| ------ | ------ | ---- | ------------------ | 2418| host | string | Yes | Host name to resolve.| 2419 2420**Return value** 2421 2422| Type | Description | 2423| ------------------------------------------- | ----------------------------- | 2424| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.| 2425 2426**Error codes** 2427 2428For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 2429 2430| ID| Error Message | 2431| ------- | --------------------------------- | 2432| 201 | Permission denied. | 2433| 401 | Parameter error. | 2434| 2100001 | Invalid parameter value. | 2435| 2100002 | Failed to connect to the service. | 2436| 2100003 | System internal error. | 2437 2438**Example** 2439 2440```ts 2441import { connection } from '@kit.NetworkKit'; 2442 2443connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2444 if (netHandle.netId == 0) { 2445 // If no network is connected, the obtained netid of netHandler is 0, which is abnormal. You can add specific processing based on the service requirements. 2446 return; 2447 } 2448 let host = "xxxx"; 2449 netHandle.getAddressesByName(host).then((data: connection.NetAddress[]) => { 2450 console.info("Succeeded to get data: " + JSON.stringify(data)); 2451 }); 2452}); 2453``` 2454 2455### getAddressByName 2456 2457getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void 2458 2459Resolves the host name by using the corresponding network to obtain the first IP address. This API uses an asynchronous callback to return the result. 2460 2461**Required permissions**: ohos.permission.INTERNET 2462 2463**System capability**: SystemCapability.Communication.NetManager.Core 2464 2465**Parameters** 2466 2467| Name | Type | Mandatory| Description | 2468| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 2469| host | string | Yes | Host name to resolve. | 2470| callback | AsyncCallback\<[NetAddress](#netaddress)> | Yes | Callback used to return the result. If the first IP address is obtained successfully, **error** is **undefined**, and **data** is the first obtained IP address. Otherwise, **error** is an error object.| 2471 2472**Error codes** 2473 2474For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 2475 2476| ID| Error Message | 2477| ------- | --------------------------------- | 2478| 201 | Permission denied. | 2479| 401 | Parameter error. | 2480| 2100001 | Invalid parameter value. | 2481| 2100002 | Failed to connect to the service. | 2482| 2100003 | System internal error. | 2483 2484**Example** 2485 2486```ts 2487import { connection } from '@kit.NetworkKit'; 2488import { BusinessError } from '@kit.BasicServicesKit'; 2489 2490connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2491 if (netHandle.netId == 0) { 2492 // If no network is connected, the obtained netid of netHandler is 0, which is abnormal. You can add specific processing based on the service requirements. 2493 return; 2494 } 2495 let host = "xxxx"; 2496 netHandle.getAddressByName(host, (error: BusinessError, data: connection.NetAddress) => { 2497 if (error) { 2498 console.error(`Failed to get address. Code:${error.code}, message:${error.message}`); 2499 return; 2500 } 2501 console.info("Succeeded to get data: " + JSON.stringify(data)); 2502 }); 2503}); 2504``` 2505 2506### getAddressByName 2507 2508getAddressByName(host: string): Promise\<NetAddress> 2509 2510Resolves the host name by using the corresponding network to obtain the first IP address. This API uses a promise to return the result. 2511 2512**Required permissions**: ohos.permission.INTERNET 2513 2514**System capability**: SystemCapability.Communication.NetManager.Core 2515 2516**Parameters** 2517 2518| Name| Type | Mandatory| Description | 2519| ------ | ------ | ---- | ------------------ | 2520| host | string | Yes | Host name to resolve.| 2521 2522**Return value** 2523 2524| Type | Description | 2525| ----------------------------------- | ------------------------------- | 2526| Promise\<[NetAddress](#netaddress)> | Promise used to return the result.| 2527 2528**Error codes** 2529 2530For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 2531 2532| ID| Error Message | 2533| ------- | --------------------------------- | 2534| 201 | Permission denied. | 2535| 401 | Parameter error. | 2536| 2100001 | Invalid parameter value. | 2537| 2100002 | Failed to connect to the service. | 2538| 2100003 | System internal error. | 2539 2540**Example** 2541 2542```ts 2543import { connection } from '@kit.NetworkKit'; 2544 2545connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2546 if (netHandle.netId == 0) { 2547 // If no network is connected, the obtained netid of netHandler is 0, which is abnormal. You can add specific processing based on the service requirements. 2548 return; 2549 } 2550 let host = "xxxx"; 2551 netHandle.getAddressByName(host).then((data: connection.NetAddress) => { 2552 console.info("Succeeded to get data: " + JSON.stringify(data)); 2553 }); 2554}); 2555``` 2556 2557## NetCap 2558 2559Defines the network capability. 2560 2561**System capability**: SystemCapability.Communication.NetManager.Core 2562 2563| Name | Value | Description | 2564| ------------------------ | ---- | ---------------------- | 2565| NET_CAPABILITY_MMS | 0 | The network can connect to the carrier's Multimedia Messaging Service Center (MMSC) to send and receive multimedia messages.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 2566| NET_CAPABILITY_NOT_METERED | 11 | The network traffic is not metered.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 2567| NET_CAPABILITY_INTERNET | 12 | The network is capable of Internet access but the network connectivity is not successfully verified by the network management module. This capability is configured by the network provider.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 2568| NET_CAPABILITY_NOT_VPN | 15 | The network does not use a virtual private network (VPN).<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 2569| NET_CAPABILITY_VALIDATED | 16 | The network management module successfully connects to the Huawei Cloud address through the network. This capability is configured by the network management module.<br>If the network management module fails to connect to the Huawei Cloud address, this flag is not available in the network capability, but this does not mean a complete loss in Internet access. Note that for a newly connected network, this value may not reflect the actual verification result as network connectivity verification is in progress. You can use **NET_CAPABILITY_CHECKING_CONNECTIVITY**<sup>12+</sup> to check whether network connectivity verification is in progress.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 2570| NET_CAPABILITY_PORTAL<sup>12+</sup> | 17 | The network is found to have a captive portal and user login authentication is required. This capability is set by the connection management module.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 2571| NET_CAPABILITY_CHECKING_CONNECTIVITY<sup>12+</sup> | 31 | The network management module is verifying the network connectivity. This value remains valid until the connectivity check is complete. If it is present, the value of **NET_CAPABILITY_VALIDATED** may be incorrect.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 2572 2573## NetBearType 2574 2575Enumerates network types. 2576 2577**System capability**: SystemCapability.Communication.NetManager.Core 2578 2579| Name | Value | Description | 2580| ----------------------- | ---- | ---------- | 2581| BEARER_CELLULAR | 0 | Cellular network.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 2582| BEARER_WIFI | 1 | Wi-Fi network.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 2583| BEARER_BLUETOOTH<sup>12+</sup> | 2 | Bluetooth network.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 2584| BEARER_ETHERNET | 3 | Ethernet network.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 2585| BEARER_VPN<sup>12+</sup>| 4 | VPN. | 2586 2587## HttpProxy<sup>10+</sup> 2588 2589Represents the HTTP proxy configuration. 2590 2591**System capability**: SystemCapability.Communication.NetManager.Core 2592 2593| Name | Type | Mandatory| Description | 2594| ------ | ------ | --- |------------------------- | 2595| host | string | Yes | Host name of the proxy server.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 2596| port | number | Yes | Host port.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 2597| exclusionList | Array\<string\> | Yes | List of the names of hosts that do not use a proxy. Host names can be domain names, IP addresses, or wildcards. The detailed matching rules are as follows:<br>- Domain name matching:<br> - Exact match: The host name of the proxy server exactly matches any host name in the list.<br> - Partial match: The host name of the proxy server contains any host name in the list.<br>For example, if **ample.com** is set in the host name list, **ample.com**, **www.ample.com**, and **ample.com:80** are matched, and **www.example.com** and **ample.com.org** are not matched.<br>- IP address matching: The host name of the proxy server exactly matches any IP address in the list.<br>- Both the domain name and IP address are added to the list for matching.<br>- A single asterisk (*) is the only valid wildcard. If the list contains only wildcards, the wildcards match all host names; that is, the HTTP proxy is disabled. A wildcard can only be added independently. It cannot be added to the list together with other domain names or IP addresses. Otherwise, the wildcard does not take effect.<br>- Host names are case insensitive.<br>- Protocol prefixes such as **http** and **https** are ignored during matching.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 2598| username<sup>12+</sup> | string | No| Name of the user who uses the proxy.| 2599| password<sup>12+</sup> | string | No| Password of the user who uses the proxy.| 2600 2601## NetSpecifier 2602 2603Provides an instance that bears data network capabilities. 2604 2605**Atomic service API**: This API can be used in atomic services since API version 11. 2606 2607**System capability**: SystemCapability.Communication.NetManager.Core 2608 2609| Name | Type | Mandatory | Description | 2610| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 2611| netCapabilities | [NetCapabilities](#netcapabilities) | Yes | Network transmission capabilities and bearer types of the data network. | 2612| bearerPrivateIdentifier | string | No | Network identifier. The identifier of the cellular network is **slot0** for SIM card 1 and **slot1** for SIM card 2. Since API version 12, you can pass the registered WLAN hotspot to the API to specify the WLAN network to be activated.| 2613 2614**Example** 2615 2616```ts 2617import { connection } from '@kit.NetworkKit'; 2618import { wifiManager } from '@kit.ConnectivityKit'; 2619import { BusinessError } from '@kit.BasicServicesKit'; 2620 2621let config: wifiManager.WifiDeviceConfig = { 2622 ssid: "TEST", 2623 preSharedKey: "**********", 2624 securityType: wifiManager.WifiSecurityType.WIFI_SEC_TYPE_PSK 2625}; 2626// Obtain the network ID of the registered WLAN through wifiManager.addCandidateConfig. 2627let networkId: number = await wifiManager.addCandidateConfig(config); 2628let netConnectionWlan = connection.createNetConnection({ 2629 netCapabilities: { 2630 bearerTypes: [connection.NetBearType.BEARER_WIFI] 2631 }, 2632 bearerPrivateIdentifier: `${networkId}` 2633}); 2634netConnectionWlan.register((error: BusinessError) => { 2635 console.log(JSON.stringify(error)); 2636}); 2637``` 2638 2639## NetCapabilityInfo<sup>10+</sup> 2640 2641Provides an instance that bears data network capabilities. 2642 2643**Atomic service API**: This API can be used in atomic services since API version 11. 2644 2645**System capability**: SystemCapability.Communication.NetManager.Core 2646 2647| Name | Type | Mandatory | Description | 2648| ----------------------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 2649| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network. | 2650| netCap | [NetCapabilities](#netcapabilities) | Yes | Network transmission capabilities and bearer types of the data network. | 2651 2652## NetCapabilities 2653 2654Defines the network capability set. 2655 2656**System capability**: SystemCapability.Communication.NetManager.Core 2657 2658| Name | Type | Mandatory| Description | 2659| --------------------- | ---------------------------------- | --- | ------------------------ | 2660| linkUpBandwidthKbps | number | No| Uplink (device-to-network) bandwidth, in kbit/s. The value **0** indicates that the network bandwidth cannot be evaluated.| 2661| linkDownBandwidthKbps | number | No| Downlink (network-to-device) bandwidth, in kbit/s. The value **0** indicates that the network bandwidth cannot be evaluated.| 2662| networkCap | Array\<[NetCap](#netcap)> | No| Network capability.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 2663| bearerTypes | Array\<[NetBearType](#netbeartype)> | Yes| Network type. The array contains only one specific network type.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 2664 2665## NetConnectionPropertyInfo<sup>11+</sup> 2666 2667Defines the network connection properties. 2668 2669**System capability**: SystemCapability.Communication.NetManager.Core 2670 2671### Attributes 2672 2673| Name | Type | Mandatory| Description | 2674| -------------------- | --------------------------------------------------- | ---- |----------------------- | 2675| netHandle | [NetHandle](#nethandle) | Yes |Data network handle.| 2676| connectionProperties | [ConnectionProperties](#connectionproperties) | Yes |Network connection properties. | 2677 2678## NetBlockStatusInfo<sup>11+</sup> 2679 2680Obtains the network block status information. 2681 2682**System capability**: SystemCapability.Communication.NetManager.Core 2683 2684### Attributes 2685 2686| Name | Type | Mandatory| Description | 2687| -------------------- | ------------------------------------- | --- |--------------------------- | 2688| netHandle | [NetHandle](#nethandle) | Yes |Data network handle. | 2689| blocked | boolean | Yes |Whether the current network is blocked.| 2690 2691## ConnectionProperties 2692 2693Defines the network connection properties. 2694 2695**System capability**: SystemCapability.Communication.NetManager.Core 2696 2697| Name | Type | Mandatory| Description | 2698| ------------- | ----------------------------------- | ----|--------------------------------------- | 2699| interfaceName | string | Yes|Network interface card (NIC) name. | 2700| domains | string | Yes|Domain name. | 2701| linkAddresses | Array\<[LinkAddress](#linkaddress)> | Yes|Link information. | 2702| routes | Array\<[RouteInfo](#routeinfo)> | Yes|Route information. | 2703| dnses | Array\<[NetAddress](#netaddress)> | Yes|Network address. For details, see [NetAddress](#netaddress).| 2704| mtu | number | Yes|Maximum transmission unit (MTU). | 2705 2706## RouteInfo 2707 2708Defines network route information. 2709 2710**System capability**: SystemCapability.Communication.NetManager.Core 2711 2712| Name | Type | Mandatory| Description | 2713| -------------- | --------------------------- | --- |-------------- | 2714| interface | string | Yes|NIC name. | 2715| destination | [LinkAddress](#linkaddress) | Yes|Destination address. | 2716| gateway | [NetAddress](#netaddress) | Yes|Gateway address. | 2717| hasGateway | boolean | Yes|Whether a gateway is present. | 2718| isDefaultRoute | boolean | Yes|Whether the route is the default route.| 2719 2720## LinkAddress 2721 2722Defines network link information. 2723 2724**System capability**: SystemCapability.Communication.NetManager.Core 2725 2726| Name | Type | Mandatory| Description | 2727| ------------ | ------------------------- |---- |-------------------- | 2728| address | [NetAddress](#netaddress) | Yes | Link address. | 2729| prefixLength | number | Yes |Length of the link address prefix. | 2730 2731## NetAddress 2732 2733Defines a network address. 2734 2735**Atomic service API**: This API can be used in atomic services since API version 12. 2736 2737**System capability**: SystemCapability.Communication.NetManager.Core 2738 2739| Name | Type |Mandatory| Description | 2740| ------- | ------ | -- |---------------------------- | 2741| address | string | Yes|Network address. | 2742| family | number | No|Address family identifier. The value is **1** for IPv4 and **2** for IPv6. The default value is **1**.| 2743| port | number | No|Port number. The value ranges from **0** to **65535**. | 2744 2745## HttpRequest 2746 2747type HttpRequest = http.HttpRequest 2748 2749Defines an HTTP request. 2750 2751**Atomic service API**: This API can be used in atomic services since API version 11. 2752 2753**System capability**: SystemCapability.Communication.NetStack 2754 2755| Type | Description | 2756| ---------------- | --------------------------- | 2757| http.HttpRequest | HTTP request task. You need to obtain an HTTP request task before calling **HttpRequest** APIs .| 2758 2759## TCPSocket 2760 2761type TCPSocket = socket.TCPSocket 2762 2763Defines a **TCPSocket** object. 2764 2765**Atomic service API**: This API can be used in atomic services since API version 10. 2766 2767**System capability**: SystemCapability.Communication.NetStack 2768 2769| Type | Description | 2770| ---------------- | --------------------------- | 2771| socket.TCPSocket | **TCPSocket** object. | 2772 2773## UDPSocket 2774 2775type UDPSocket = socket.UDPSocket 2776 2777Defines a **UDPSocket** object. 2778 2779**Atomic service API**: This API can be used in atomic services since API version 10. 2780 2781**System capability**: SystemCapability.Communication.NetStack 2782 2783| Type | Description | 2784| ---------------- | --------------------------- | 2785| socket.UDPSocket | **UDPSocket** object. | 2786