1# @ohos.net.connection (Network Connection Management) 2 3The **connection** module provides basic network management capabilities. With the APIs provided by this module, 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 not 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**Atomic service API**: This API can be used in atomic services since API version 15. 1565 1566**System capability**: SystemCapability.Communication.NetManager.Core 1567 1568**Parameters** 1569 1570| Name | Type | Mandatory| Description | 1571| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1572| host | string | Yes | Name of the custom host. | 1573| ip | Array\<string> | Yes | List of IP addresses mapped to the host name. | 1574| 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.| 1575 1576**Error codes** 1577 1578For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1579 1580| ID| Error Message | 1581| ------- | --------------------------------- | 1582| 201 | Permission denied. | 1583| 401 | Parameter error. | 1584| 2100001 | Invalid parameter value. | 1585| 2100002 | Failed to connect to the service. | 1586| 2100003 | System internal error. | 1587 1588**Example** 1589 1590```ts 1591import { connection } from '@kit.NetworkKit'; 1592import { BusinessError } from '@kit.BasicServicesKit'; 1593 1594connection.addCustomDnsRule("xxxx", ["xx.xx.xx.xx","xx.xx.xx.xx"], (error: BusinessError, data: void) => { 1595 if (error) { 1596 console.error(`Failed to get add custom dns rule. Code:${error.code}, message:${error.message}`); 1597 return; 1598 } 1599 console.info("Succeeded to get data: " + JSON.stringify(data)); 1600}) 1601``` 1602 1603## connection.addCustomDnsRule<sup>11+</sup> 1604 1605addCustomDnsRule(host: string, ip: Array\<string\>): Promise\<void\> 1606 1607Adds custom DNS rules for the specified host of the current application. This API uses a promise to return the result. 1608 1609**Required permissions**: ohos.permission.INTERNET 1610 1611**Atomic service API**: This API can be used in atomic services since API version 15. 1612 1613**System capability**: SystemCapability.Communication.NetManager.Core 1614 1615**Parameters** 1616 1617| Name| Type | Mandatory| Description | 1618| ------ | -------------- | ---- | -------------------------- | 1619| host | string | Yes | Name of the custom host. | 1620| ip | Array\<string> | Yes | List of IP addresses mapped to the host name.| 1621 1622**Return value** 1623 1624| Type | Description | 1625| ---------------------- | ----------------------- | 1626| Promise\<Array\<void>> | Promise that returns no value.| 1627 1628**Error codes** 1629 1630For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1631 1632| ID| Error Message | 1633| ------- | --------------------------------- | 1634| 201 | Permission denied. | 1635| 401 | Parameter error. | 1636| 2100001 | Invalid parameter value. | 1637| 2100002 | Failed to connect to the service. | 1638| 2100003 | System internal error. | 1639 1640**Example** 1641 1642```ts 1643import { connection } from '@kit.NetworkKit'; 1644import { BusinessError } from '@kit.BasicServicesKit'; 1645 1646connection.addCustomDnsRule("xxxx", ["xx.xx.xx.xx","xx.xx.xx.xx"]).then(() => { 1647 console.info("success"); 1648}).catch((error: BusinessError) => { 1649 console.error(JSON.stringify(error)); 1650}) 1651``` 1652 1653## connection.removeCustomDnsRule<sup>11+</sup> 1654 1655removeCustomDnsRule(host: string, callback: AsyncCallback\<void\>): void 1656 1657Removes the custom DNS rules of the specified host from the current application. This API uses an asynchronous callback to return the result. 1658 1659**Required permissions**: ohos.permission.INTERNET 1660 1661**Atomic service API**: This API can be used in atomic services since API version 15. 1662 1663**System capability**: SystemCapability.Communication.NetManager.Core 1664 1665**Parameters** 1666 1667| Name | Type | Mandatory| Description | 1668| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1669| host | string | Yes | Name of the host for which DNS rules are to be deleted. | 1670| 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.| 1671 1672**Error codes** 1673 1674For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1675 1676| ID| Error Message | 1677| ------- | ----------------------------- | 1678| 201 | Permission denied. | 1679| 401 | Parameter error. | 1680| 2100001 | Invalid parameter value. | 1681| 2100002 | Failed to connect to the service. | 1682| 2100003 | System internal error. | 1683 1684**Example** 1685 1686```ts 1687import { connection } from '@kit.NetworkKit'; 1688import { BusinessError } from '@kit.BasicServicesKit'; 1689 1690connection.removeCustomDnsRule("xxxx", (error: BusinessError, data: void) => { 1691 if (error) { 1692 console.error(`Failed to remove custom dns rule. Code:${error.code}, message:${error.message}`); 1693 return; 1694 } 1695 console.info("Succeeded to get data: " + JSON.stringify(data)); 1696}) 1697``` 1698 1699## connection.removeCustomDnsRule<sup>11+</sup> 1700 1701removeCustomDnsRule(host: string): Promise\<void\> 1702 1703Removes the custom DNS rules of the specified host from the current application. This API uses a promise to return the result. 1704 1705**Required permissions**: ohos.permission.INTERNET 1706 1707**Atomic service API**: This API can be used in atomic services since API version 15. 1708 1709**System capability**: SystemCapability.Communication.NetManager.Core 1710 1711**Parameters** 1712 1713| Name| Type | Mandatory| Description | 1714| ------ | ------ | ---- | ------------------------------- | 1715| host | string | Yes | Name of the host for which DNS rules are to be deleted.| 1716 1717**Return value** 1718 1719| Type | Description | 1720| ---------------------- | ----------------------- | 1721| Promise\<Array\<void>> | Promise that returns no value.| 1722 1723**Error codes** 1724 1725For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1726 1727| ID| Error Message | 1728| ------- | --------------------------------- | 1729| 201 | Permission denied. | 1730| 401 | Parameter error. | 1731| 2100001 | Invalid parameter value. | 1732| 2100002 | Failed to connect to the service. | 1733| 2100003 | System internal error. | 1734 1735**Example** 1736 1737```ts 1738import { connection } from '@kit.NetworkKit'; 1739import { BusinessError } from '@kit.BasicServicesKit'; 1740 1741connection.removeCustomDnsRule("xxxx").then(() => { 1742 console.log("success"); 1743}).catch((error: BusinessError) => { 1744 console.log(JSON.stringify(error)); 1745}) 1746``` 1747 1748## connection.clearCustomDnsRules<sup>11+</sup> 1749 1750clearCustomDnsRules(callback: AsyncCallback\<void\>): void 1751 1752Removes all custom DNS rules from the current application. This API uses an asynchronous callback to return the result. 1753 1754**Required permissions**: ohos.permission.INTERNET 1755 1756**System capability**: SystemCapability.Communication.NetManager.Core 1757 1758**Parameters** 1759 1760| Name | Type | Mandatory| Description | 1761| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1762| 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.| 1763 1764**Error codes** 1765 1766For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1767 1768| ID| Error Message | 1769| ------- | --------------------------------- | 1770| 201 | Permission denied. | 1771| 401 | Parameter error. | 1772| 2100001 | Invalid parameter value. | 1773| 2100002 | Failed to connect to the service. | 1774| 2100003 | System internal error. | 1775 1776**Example** 1777 1778```ts 1779import { connection } from '@kit.NetworkKit'; 1780import { BusinessError } from '@kit.BasicServicesKit'; 1781 1782connection.clearCustomDnsRules((error: BusinessError, data: void) => { 1783 if (error) { 1784 console.error(`Failed to clear custom dns rules. Code:${error.code}, message:${error.message}`); 1785 return; 1786 } 1787 console.info("Succeeded to get data: " + JSON.stringify(data)); 1788}) 1789``` 1790 1791## connection.clearCustomDnsRules<sup>11+</sup> 1792 1793clearCustomDnsRules(): Promise\<void\> 1794 1795Removes all custom DNS rules from the current application. This API uses a promise to return the result. 1796 1797**Required permissions**: ohos.permission.INTERNET 1798 1799**System capability**: SystemCapability.Communication.NetManager.Core 1800 1801**Return value** 1802 1803| Type | Description | 1804| ---------------------- | ----------------------- | 1805| Promise\<void\> | Promise that returns no value. | 1806 1807**Error codes** 1808 1809For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1810 1811| ID| Error Message | 1812| ------- | --------------------------------- | 1813| 201 | Permission denied. | 1814| 2100001 | Invalid parameter value. | 1815| 2100002 | Failed to connect to the service. | 1816| 2100003 | System internal error. | 1817 1818**Example** 1819 1820```ts 1821import { connection } from '@kit.NetworkKit'; 1822import { BusinessError } from '@kit.BasicServicesKit'; 1823 1824connection.clearCustomDnsRules().then(() => { 1825 console.log("success"); 1826}).catch((error: BusinessError) => { 1827 console.log(JSON.stringify(error)); 1828}) 1829``` 1830 1831## connection.setPacUrl<sup>15+</sup> 1832 1833setPacUrl(pacUrl: string): void 1834 1835Sets the URL of the system-level proxy auto-config (PAC) script. 1836 1837**Required permissions**: ohos.permission.SET_PAC_URL 1838 1839**System capability**: SystemCapability.Communication.NetManager.Core 1840 1841**Parameters** 1842 1843| Name | Type | Mandatory| Description | 1844| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 1845| pacUrl | string | Yes | URL of the PAC script. Note that this URL will not be verified by the API. | 1846 1847**Error codes** 1848 1849For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1850 1851| ID| Error Message | 1852| ------- | --------------------------------- | 1853| 201 | Permission denied. | 1854| 401 | Parameter error. | 1855| 2100002 | Failed to connect to the service. | 1856| 2100003 | System internal error. | 1857 1858**Example** 1859 1860```ts 1861import { connection } from '@kit.NetworkKit'; 1862 1863let pacUrl = "xxx"; 1864connection.setPacUrl(pacUrl); 1865``` 1866 1867## connection.getPacUrl<sup>15+</sup> 1868 1869getPacUrl(): string 1870 1871Obtains the URL of the system-level PAC script. 1872 1873**System capability**: SystemCapability.Communication.NetManager.Core 1874 1875**Return value** 1876 1877| Type | Description | 1878| ---------------------- | ----------------------- | 1879| string | URL of the PAC script. If the URL does not exist, the error code 2100003 is returned. | 1880 1881**Error codes** 1882 1883For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1884 1885| ID| Error Message | 1886| ------- | --------------------------------- | 1887| 2100002 | Failed to connect to the service. | 1888| 2100003 | System internal error. | 1889 1890**Example** 1891 1892```ts 1893import { connection } from '@kit.NetworkKit'; 1894 1895let pacUrl = connection.getPacUrl(); 1896``` 1897 1898 1899## NetConnection 1900 1901Represents the network connection handle. 1902 1903> **NOTE** 1904> When a device changes to the network connected state, the **netAvailable**, **netCapabilitiesChange**, and **netConnectionPropertiesChange** events will be triggered. 1905> When a device changes to the network disconnected state, the **netLost** event will be triggered. 1906> 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. 1907 1908### register 1909 1910register(callback: AsyncCallback\<void>): void 1911 1912Registers a listener for network status changes. 1913 1914**Required permission**: ohos.permission.GET_NETWORK_INFO 1915 1916**Atomic service API**: This API can be used in atomic services since API version 11. 1917 1918**System capability**: SystemCapability.Communication.NetManager.Core 1919 1920**Parameters** 1921 1922| Name | Type | Mandatory| Description | 1923| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1924| 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.| 1925 1926**Error codes** 1927 1928For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1929 1930| ID| Error Message | 1931| ------- | ---------------------------------------------------- | 1932| 201 | Permission denied. | 1933| 401 | Parameter error. | 1934| 2100002 | Failed to connect to the service. | 1935| 2100003 | System internal error. | 1936| 2101008 | The callback already exists. | 1937| 2101022 | The number of requests exceeded the maximum allowed. | 1938 1939**Example** 1940 1941```ts 1942import { connection } from '@kit.NetworkKit'; 1943import { BusinessError } from '@kit.BasicServicesKit'; 1944 1945let netCon: connection.NetConnection = connection.createNetConnection(); 1946netCon.register((error: BusinessError) => { 1947 console.log(JSON.stringify(error)); 1948}); 1949``` 1950 1951### unregister 1952 1953unregister(callback: AsyncCallback\<void>): void 1954 1955Unregisters the listener for network status changes. 1956 1957**Atomic service API**: This API can be used in atomic services since API version 11. 1958 1959**System capability**: SystemCapability.Communication.NetManager.Core 1960 1961**Parameters** 1962 1963| Name | Type | Mandatory| Description | 1964| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1965| 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.| 1966 1967**Error codes** 1968 1969For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 1970 1971| ID| Error Message | 1972| ------- | --------------------------------- | 1973| 401 | Parameter error. | 1974| 2100002 | Failed to connect to the service. | 1975| 2100003 | System internal error. | 1976| 2101007 | The callback does not exist. | 1977 1978**Example** 1979 1980```ts 1981import { connection } from '@kit.NetworkKit'; 1982import { BusinessError } from '@kit.BasicServicesKit'; 1983 1984let netCon: connection.NetConnection = connection.createNetConnection(); 1985netCon.unregister((error: BusinessError) => { 1986 console.log(JSON.stringify(error)); 1987}); 1988``` 1989 1990### on('netAvailable') 1991 1992on(type: 'netAvailable', callback: Callback\<NetHandle>): void 1993 1994Registers 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. 1995 1996**Atomic service API**: This API can be used in atomic services since API version 11. 1997 1998**System capability**: SystemCapability.Communication.NetManager.Core 1999 2000**Parameters** 2001 2002| Name | Type | Mandatory| Description | 2003| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 2004| type | string | Yes | Event type. This field has a fixed value of **netAvailable**.<br>**netAvailable**: event indicating that the data network is available.| 2005| callback | Callback\<[NetHandle](#nethandle)> | Yes | Callback used to return the network handle.| 2006 2007**Example** 2008 2009```ts 2010import { connection } from '@kit.NetworkKit'; 2011import { BusinessError } from '@kit.BasicServicesKit'; 2012 2013// Create a NetConnection object. 2014let netCon: connection.NetConnection = connection.createNetConnection(); 2015 2016// Call register to register a listener. 2017netCon.register((error: BusinessError) => { 2018 console.log(JSON.stringify(error)); 2019}); 2020 2021// Subscribe to netAvailable events. Event notifications can be received only after register is called. 2022netCon.on('netAvailable', (data: connection.NetHandle) => { 2023 console.info("Succeeded to get data: " + JSON.stringify(data)); 2024}); 2025 2026// Call unregister to unregister the listener. 2027netCon.unregister((error: BusinessError) => { 2028 console.log(JSON.stringify(error)); 2029}); 2030``` 2031 2032### on('netBlockStatusChange') 2033 2034on(type: 'netBlockStatusChange', callback: Callback\<NetBlockStatusInfo>): void 2035 2036Registers 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. 2037 2038**System capability**: SystemCapability.Communication.NetManager.Core 2039 2040**Parameters** 2041 2042| Name | Type | Mandatory| Description | 2043| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2044| type | string | Yes | Event type. This field has a fixed value of **netBlockStatusChange**.<br>**netBlockStatusChange**: event indicating a change in the network blocking status.| 2045| callback | Callback<[NetBlockStatusInfo](#netblockstatusinfo11)> | Yes | Callback used to return the result.| 2046 2047**Example** 2048 2049```ts 2050import { connection } from '@kit.NetworkKit'; 2051import { BusinessError } from '@kit.BasicServicesKit'; 2052 2053// Create a NetConnection object. 2054let netCon: connection.NetConnection = connection.createNetConnection(); 2055 2056// Call register to register a listener. 2057netCon.register((error: BusinessError) => { 2058 console.log(JSON.stringify(error)); 2059}); 2060 2061// Subscribe to netBlockStatusChange events. Event notifications can be received only after register is called. 2062netCon.on('netBlockStatusChange', (data: connection.NetBlockStatusInfo) => { 2063 console.info("Succeeded to get data: " + JSON.stringify(data)); 2064}); 2065 2066// Call unregister to unregister the listener. 2067netCon.unregister((error: BusinessError) => { 2068 console.log(JSON.stringify(error)); 2069}); 2070``` 2071 2072### on('netCapabilitiesChange') 2073 2074on(type: 'netCapabilitiesChange', callback: Callback\<NetCapabilityInfo\>): void 2075 2076Registers 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. 2077 2078**Atomic service API**: This API can be used in atomic services since API version 11. 2079 2080**System capability**: SystemCapability.Communication.NetManager.Core 2081 2082**Parameters** 2083 2084| Name | Type | Mandatory| Description | 2085| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2086| type | string | Yes | Event type. This field has a fixed value of **netCapabilitiesChange**.<br>**netCapabilitiesChange**: event indicating that the network capabilities have changed.| 2087| callback | Callback<[NetCapabilityInfo](#netcapabilityinfo10)> | Yes | Callback used to return the network handle (**netHandle**) and capability information (**netCap**).| 2088 2089**Example** 2090 2091```ts 2092import { connection } from '@kit.NetworkKit'; 2093import { BusinessError } from '@kit.BasicServicesKit'; 2094 2095// Create a NetConnection object. 2096let netCon: connection.NetConnection = connection.createNetConnection(); 2097 2098// Call register to register a listener. 2099netCon.register((error: BusinessError) => { 2100 console.log(JSON.stringify(error)); 2101}); 2102 2103// Subscribe to netCapabilitiesChange events. Event notifications can be received only after register is called. 2104netCon.on('netCapabilitiesChange', (data: connection.NetCapabilityInfo) => { 2105 console.info("Succeeded to get data: " + JSON.stringify(data)); 2106}); 2107 2108// Call unregister to unregister the listener. 2109netCon.unregister((error: BusinessError) => { 2110 console.log(JSON.stringify(error)); 2111}); 2112``` 2113 2114### on('netConnectionPropertiesChange') 2115 2116on(type: 'netConnectionPropertiesChange', callback: Callback\<NetConnectionPropertyInfo\>): void 2117 2118Registers 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. 2119 2120**System capability**: SystemCapability.Communication.NetManager.Core 2121 2122**Parameters** 2123 2124| Name | Type | Mandatory| Description | 2125| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 2126| type | string | Yes | Event type. This field has a fixed value of **netConnectionPropertiesChange**.<br>**netConnectionPropertiesChange**: event indicating that network connection properties have changed.| 2127| callback | Callback<[NetConnectionPropertyInfo](#netconnectionpropertyinfo11)> | Yes | Callback used to return the result.| 2128 2129**Example** 2130 2131```ts 2132import { connection } from '@kit.NetworkKit'; 2133import { BusinessError } from '@kit.BasicServicesKit'; 2134 2135// Create a NetConnection object. 2136let netCon: connection.NetConnection = connection.createNetConnection(); 2137 2138// Call register to register a listener. 2139netCon.register((error: BusinessError) => { 2140 console.log(JSON.stringify(error)); 2141}); 2142 2143// Subscribe to netConnectionPropertiesChange events. Event notifications can be received only after register is called. 2144netCon.on('netConnectionPropertiesChange', (data: connection.NetConnectionPropertyInfo) => { 2145 console.info("Succeeded to get data: " + JSON.stringify(data)); 2146}); 2147 2148// Call unregister to unregister the listener. 2149netCon.unregister((error: BusinessError) => { 2150 console.log(JSON.stringify(error)); 2151}); 2152``` 2153 2154### on('netLost') 2155 2156on(type: 'netLost', callback: Callback\<NetHandle>): void 2157 2158Registers 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. 2159 2160**Atomic service API**: This API can be used in atomic services since API version 11. 2161 2162**System capability**: SystemCapability.Communication.NetManager.Core 2163 2164**Parameters** 2165 2166| Name | Type | Mandatory| Description | 2167| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 2168| 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.| 2169| callback | Callback\<[NetHandle](#nethandle)> | Yes | Callback used to return **netHandle**.| 2170 2171**Example** 2172 2173```ts 2174import { connection } from '@kit.NetworkKit'; 2175import { BusinessError } from '@kit.BasicServicesKit'; 2176 2177// Create a NetConnection object. 2178let netCon: connection.NetConnection = connection.createNetConnection(); 2179 2180// Call register to register a listener. 2181netCon.register((error: BusinessError) => { 2182 console.log(JSON.stringify(error)); 2183}); 2184 2185// Subscribe to netLost events. Event notifications can be received only after register is called. 2186netCon.on('netLost', (data: connection.NetHandle) => { 2187 console.info("Succeeded to get data: " + JSON.stringify(data)); 2188}); 2189 2190// Call unregister to unregister the listener. 2191netCon.unregister((error: BusinessError) => { 2192 console.log(JSON.stringify(error)); 2193}); 2194``` 2195 2196### on('netUnavailable') 2197 2198on(type: 'netUnavailable', callback: Callback\<void>): void 2199 2200Registers 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. 2201 2202**Atomic service API**: This API can be used in atomic services since API version 11. 2203 2204**System capability**: SystemCapability.Communication.NetManager.Core 2205 2206**Parameters** 2207 2208| Name | Type | Mandatory| Description | 2209| -------- | --------------- | ---- | ------------------------------------------------------------ | 2210| type | string | Yes | Event type. This field has a fixed value of **netUnavailable**.<br>**netUnavailable**: event indicating that the network is unavailable.| 2211| callback | Callback\<void> | Yes | Callback used to return the result, which is empty.| 2212 2213**Example** 2214 2215```ts 2216import { connection } from '@kit.NetworkKit'; 2217import { BusinessError } from '@kit.BasicServicesKit'; 2218 2219// Create a NetConnection object. 2220let netCon: connection.NetConnection = connection.createNetConnection(); 2221 2222// Call register to register a listener. 2223netCon.register((error: BusinessError) => { 2224 console.log(JSON.stringify(error)); 2225}); 2226 2227// Subscribe to netUnavailable events. Event notifications can be received only after register is called. 2228netCon.on('netUnavailable', () => { 2229 console.info("Succeeded to get unavailable net event"); 2230}); 2231 2232// Call unregister to unregister the listener. 2233netCon.unregister((error: BusinessError) => { 2234 console.log(JSON.stringify(error)); 2235}); 2236``` 2237 2238## NetHandle 2239 2240Defines the handle of the data network. 2241 2242Before invoking **NetHandle** APIs, call **getNetHandle** to obtain a **NetHandle** object. 2243 2244**System capability**: SystemCapability.Communication.NetManager.Core 2245 2246### Attributes 2247 2248| Name | Type | Mandatory| Description | 2249| ------ | ------ | --- |------------------------- | 2250| 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.| 2251 2252### bindSocket<sup>9+</sup> 2253 2254bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void 2255 2256Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses an asynchronous callback to return the result. 2257 2258**System capability**: SystemCapability.Communication.NetManager.Core 2259 2260**Parameters** 2261 2262| Name | Type | Mandatory| Description | 2263| ----------- | ------------------------ | ---- | -------------------------------| 2264| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes| **TCPSocket** or **UDPSocket** object.| 2265| 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.| 2266 2267**Error codes** 2268 2269For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 2270 2271| ID| Error Message | 2272| ------- | --------------------------------- | 2273| 401 | Parameter error. | 2274| 2100001 | Invalid parameter value. | 2275| 2100002 | Failed to connect to the service. | 2276| 2100003 | System internal error. | 2277 2278**Example** 2279 2280```ts 2281import { connection, socket } from '@kit.NetworkKit'; 2282import { BusinessError } from '@kit.BasicServicesKit'; 2283 2284interface Data { 2285 message: ArrayBuffer, 2286 remoteInfo: socket.SocketRemoteInfo 2287} 2288 2289 connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2290 if (netHandle.netId == 0) { 2291 // 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. 2292 } 2293 let tcp : socket.TCPSocket = socket.constructTCPSocketInstance(); 2294 let udp : socket.UDPSocket = socket.constructUDPSocketInstance(); 2295 let socketType = "TCPSocket"; 2296 if (socketType == "TCPSocket") { 2297 tcp.bind({address:"192.168.xxx.xxx", 2298 port:8080, 2299 family:1} as socket.NetAddress, (error: Error) => { 2300 if (error) { 2301 console.log('bind fail'); 2302 return; 2303 } 2304 netHandle.bindSocket(tcp, (error: BusinessError, data: void) => { 2305 if (error) { 2306 console.error(`Failed to bind socket. Code:${error.code}, message:${error.message}`); 2307 return; 2308 } else { 2309 console.info(JSON.stringify(data)); 2310 } 2311 }); 2312 }); 2313 } else { 2314 let callback: (value: Data) => void = (value: Data) => { 2315 console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); 2316 }; 2317 udp.bind({address:"192.168.xxx.xxx", 2318 port:8080, 2319 family:1} as socket.NetAddress, (error: BusinessError) => { 2320 if (error) { 2321 console.error(`Failed to bind. Code:${error.code}, message:${error.message}`); 2322 return; 2323 } 2324 udp.on('message', (data: Data) => { 2325 console.info("Succeeded to get data: " + JSON.stringify(data)); 2326 }); 2327 netHandle.bindSocket(udp, (error: BusinessError, data: void) => { 2328 if (error) { 2329 console.error(`Failed to bind socket. Code:${error.code}, message:${error.message}`); 2330 return; 2331 } else { 2332 console.info(JSON.stringify(data)); 2333 } 2334 }); 2335 }); 2336 } 2337}) 2338``` 2339 2340### bindSocket<sup>9+</sup> 2341 2342bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void\> 2343 2344Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses a promise to return the result. 2345 2346**System capability**: SystemCapability.Communication.NetManager.Core 2347 2348**Parameters** 2349 2350| Name | Type | Mandatory | Description | 2351| --------------- | --------------------- | ---- | ------------------------------ | 2352| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes | **TCPSocket** or **UDPSocket** object.| 2353 2354**Return value** 2355 2356| Type | Description | 2357| -------------- | ---------------------- | 2358| Promise\<void> | Promise that returns no value.| 2359 2360**Error codes** 2361 2362For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 2363 2364| ID| Error Message | 2365| ------- | --------------------------------- | 2366| 401 | Parameter error. | 2367| 2100001 | Invalid parameter value. | 2368| 2100002 | Failed to connect to the service. | 2369| 2100003 | System internal error. | 2370 2371**Example** 2372 2373```ts 2374import { connection, socket } from '@kit.NetworkKit'; 2375import { BusinessError } from '@kit.BasicServicesKit'; 2376 2377interface Data { 2378 message: ArrayBuffer, 2379 remoteInfo: socket.SocketRemoteInfo 2380} 2381 2382connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2383 if (netHandle.netId == 0) { 2384 // 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. 2385 return; 2386 } 2387 let tcp : socket.TCPSocket = socket.constructTCPSocketInstance(); 2388 let udp : socket.UDPSocket = socket.constructUDPSocketInstance(); 2389 let socketType = "TCPSocket"; 2390 if (socketType == "TCPSocket") { 2391 tcp.bind({address:"192.168.xxx.xxx", 2392 port:8080, 2393 family:1} as socket.NetAddress, (error: Error) => { 2394 if (error) { 2395 console.log('bind fail'); 2396 return; 2397 } 2398 netHandle.bindSocket(tcp).then(() => { 2399 console.info("bind socket success"); 2400 }).catch((error: BusinessError) => { 2401 console.error(`Failed to bind socket. Code:${error.code}, message:${error.message}`); 2402 }); 2403 }); 2404 } else { 2405 let callback: (value: Data) => void = (value: Data) => { 2406 console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); 2407 } 2408 udp.bind({address:"192.168.xxx.xxx", 2409 port:8080, 2410 family:1} as socket.NetAddress, (error: BusinessError) => { 2411 if (error) { 2412 console.error(`Failed to bind. Code:${error.code}, message:${error.message}`); 2413 return; 2414 } 2415 udp.on('message', (data: Data) => { 2416 console.info("Succeeded to get data: " + JSON.stringify(data)); 2417 }); 2418 netHandle.bindSocket(udp).then(() => { 2419 console.info("bind socket success"); 2420 }).catch((error: BusinessError) => { 2421 console.error(`Failed to bind socket. Code:${error.code}, message:${error.message}`); 2422 }); 2423 }); 2424 } 2425}); 2426``` 2427 2428### getAddressesByName 2429 2430getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>\>\): void 2431 2432Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses an asynchronous callback to return the result. 2433 2434**Required permissions**: ohos.permission.INTERNET 2435 2436**Atomic service API**: This API can be used in atomic services since API version 15. 2437 2438**System capability**: SystemCapability.Communication.NetManager.Core 2439 2440**Parameters** 2441 2442| Name | Type | Mandatory| Description | 2443| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 2444| host | string | Yes | Host name to resolve. | 2445| 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.| 2446 2447**Error codes** 2448 2449For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 2450 2451| ID| Error Message | 2452| ------- | --------------------------------- | 2453| 201 | Permission denied. | 2454| 401 | Parameter error. | 2455| 2100001 | Invalid parameter value. | 2456| 2100002 | Failed to connect to the service. | 2457| 2100003 | System internal error. | 2458 2459**Example** 2460 2461```ts 2462import { connection } from '@kit.NetworkKit'; 2463import { BusinessError } from '@kit.BasicServicesKit'; 2464 2465connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2466 if (netHandle.netId == 0) { 2467 // 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. 2468 return; 2469 } 2470 let host = "xxxx"; 2471 netHandle.getAddressesByName(host, (error: BusinessError, data: connection.NetAddress[]) => { 2472 if (error) { 2473 console.error(`Failed to get addresses. Code:${error.code}, message:${error.message}`); 2474 return; 2475 } 2476 console.info("Succeeded to get data: " + JSON.stringify(data)); 2477 }); 2478}); 2479``` 2480 2481### getAddressesByName 2482 2483getAddressesByName(host: string): Promise\<Array\<NetAddress>> 2484 2485Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses a promise to return the result. 2486 2487**Required permissions**: ohos.permission.INTERNET 2488 2489**Atomic service API**: This API can be used in atomic services since API version 15. 2490 2491**System capability**: SystemCapability.Communication.NetManager.Core 2492 2493**Parameters** 2494 2495| Name| Type | Mandatory| Description | 2496| ------ | ------ | ---- | ------------------ | 2497| host | string | Yes | Host name to resolve.| 2498 2499**Return value** 2500 2501| Type | Description | 2502| ------------------------------------------- | ----------------------------- | 2503| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.| 2504 2505**Error codes** 2506 2507For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 2508 2509| ID| Error Message | 2510| ------- | --------------------------------- | 2511| 201 | Permission denied. | 2512| 401 | Parameter error. | 2513| 2100001 | Invalid parameter value. | 2514| 2100002 | Failed to connect to the service. | 2515| 2100003 | System internal error. | 2516 2517**Example** 2518 2519```ts 2520import { connection } from '@kit.NetworkKit'; 2521 2522connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2523 if (netHandle.netId == 0) { 2524 // 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. 2525 return; 2526 } 2527 let host = "xxxx"; 2528 netHandle.getAddressesByName(host).then((data: connection.NetAddress[]) => { 2529 console.info("Succeeded to get data: " + JSON.stringify(data)); 2530 }); 2531}); 2532``` 2533 2534### getAddressByName 2535 2536getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void 2537 2538Resolves the host name by using the corresponding network to obtain the first IP address. This API uses an asynchronous callback to return the result. 2539 2540**Required permissions**: ohos.permission.INTERNET 2541 2542**System capability**: SystemCapability.Communication.NetManager.Core 2543 2544**Parameters** 2545 2546| Name | Type | Mandatory| Description | 2547| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 2548| host | string | Yes | Host name to resolve. | 2549| 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.| 2550 2551**Error codes** 2552 2553For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 2554 2555| ID| Error Message | 2556| ------- | --------------------------------- | 2557| 201 | Permission denied. | 2558| 401 | Parameter error. | 2559| 2100001 | Invalid parameter value. | 2560| 2100002 | Failed to connect to the service. | 2561| 2100003 | System internal error. | 2562 2563**Example** 2564 2565```ts 2566import { connection } from '@kit.NetworkKit'; 2567import { BusinessError } from '@kit.BasicServicesKit'; 2568 2569connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2570 if (netHandle.netId == 0) { 2571 // 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. 2572 return; 2573 } 2574 let host = "xxxx"; 2575 netHandle.getAddressByName(host, (error: BusinessError, data: connection.NetAddress) => { 2576 if (error) { 2577 console.error(`Failed to get address. Code:${error.code}, message:${error.message}`); 2578 return; 2579 } 2580 console.info("Succeeded to get data: " + JSON.stringify(data)); 2581 }); 2582}); 2583``` 2584 2585### getAddressByName 2586 2587getAddressByName(host: string): Promise\<NetAddress> 2588 2589Resolves the host name by using the corresponding network to obtain the first IP address. This API uses a promise to return the result. 2590 2591**Required permissions**: ohos.permission.INTERNET 2592 2593**System capability**: SystemCapability.Communication.NetManager.Core 2594 2595**Parameters** 2596 2597| Name| Type | Mandatory| Description | 2598| ------ | ------ | ---- | ------------------ | 2599| host | string | Yes | Host name to resolve.| 2600 2601**Return value** 2602 2603| Type | Description | 2604| ----------------------------------- | ------------------------------- | 2605| Promise\<[NetAddress](#netaddress)> | Promise used to return the result.| 2606 2607**Error codes** 2608 2609For details about the error codes, see [Network Connection Management Error Codes](errorcode-net-connection.md). 2610 2611| ID| Error Message | 2612| ------- | --------------------------------- | 2613| 201 | Permission denied. | 2614| 401 | Parameter error. | 2615| 2100001 | Invalid parameter value. | 2616| 2100002 | Failed to connect to the service. | 2617| 2100003 | System internal error. | 2618 2619**Example** 2620 2621```ts 2622import { connection } from '@kit.NetworkKit'; 2623 2624connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2625 if (netHandle.netId == 0) { 2626 // 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. 2627 return; 2628 } 2629 let host = "xxxx"; 2630 netHandle.getAddressByName(host).then((data: connection.NetAddress) => { 2631 console.info("Succeeded to get data: " + JSON.stringify(data)); 2632 }); 2633}); 2634``` 2635 2636## NetCap 2637 2638Defines the network capability. 2639 2640**System capability**: SystemCapability.Communication.NetManager.Core 2641 2642| Name | Value | Description | 2643| ------------------------ | ---- | ---------------------- | 2644| 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.| 2645| 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.| 2646| 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.| 2647| 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.| 2648| 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.| 2649| 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.| 2650| 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.| 2651 2652## NetBearType 2653 2654Enumerates network types. 2655 2656**System capability**: SystemCapability.Communication.NetManager.Core 2657 2658| Name | Value | Description | 2659| ----------------------- | ---- | ---------- | 2660| BEARER_CELLULAR | 0 | Cellular network.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 2661| BEARER_WIFI | 1 | Wi-Fi network.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 2662| BEARER_BLUETOOTH<sup>12+</sup> | 2 | Bluetooth network.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 2663| BEARER_ETHERNET | 3 | Ethernet network.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 2664| BEARER_VPN<sup>12+</sup>| 4 | VPN. | 2665 2666## HttpProxy<sup>10+</sup> 2667 2668Represents the HTTP proxy configuration. 2669 2670**System capability**: SystemCapability.Communication.NetManager.Core 2671 2672| Name | Type | Mandatory| Description | 2673| ------ | ------ | --- |------------------------- | 2674| 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.| 2675| port | number | Yes | Host port.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 2676| 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.| 2677| username<sup>12+</sup> | string | No| Name of the user who uses the proxy.| 2678| password<sup>12+</sup> | string | No| Password of the user who uses the proxy.| 2679 2680## NetSpecifier 2681 2682Provides an instance that bears data network capabilities. 2683 2684**Atomic service API**: This API can be used in atomic services since API version 11. 2685 2686**System capability**: SystemCapability.Communication.NetManager.Core 2687 2688| Name | Type | Mandatory | Description | 2689| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 2690| netCapabilities | [NetCapabilities](#netcapabilities) | Yes | Network transmission capabilities and bearer types of the data network. | 2691| 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.| 2692 2693**Example** 2694 2695```ts 2696import { connection } from '@kit.NetworkKit'; 2697import { wifiManager } from '@kit.ConnectivityKit'; 2698import { BusinessError } from '@kit.BasicServicesKit'; 2699 2700let config: wifiManager.WifiDeviceConfig = { 2701 ssid: "TEST", 2702 preSharedKey: "**********", 2703 securityType: wifiManager.WifiSecurityType.WIFI_SEC_TYPE_PSK 2704}; 2705// Obtain the network ID of the registered WLAN through wifiManager.addCandidateConfig. 2706let networkId: number = await wifiManager.addCandidateConfig(config); 2707let netConnectionWlan = connection.createNetConnection({ 2708 netCapabilities: { 2709 bearerTypes: [connection.NetBearType.BEARER_WIFI] 2710 }, 2711 bearerPrivateIdentifier: `${networkId}` 2712}); 2713netConnectionWlan.register((error: BusinessError) => { 2714 console.log(JSON.stringify(error)); 2715}); 2716``` 2717 2718## NetCapabilityInfo<sup>10+</sup> 2719 2720Provides an instance that bears data network capabilities. 2721 2722**Atomic service API**: This API can be used in atomic services since API version 11. 2723 2724**System capability**: SystemCapability.Communication.NetManager.Core 2725 2726| Name | Type | Mandatory | Description | 2727| ----------------------- | ------------------------------------ | ---- | ------------------------------------------------------------ | 2728| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network. | 2729| netCap | [NetCapabilities](#netcapabilities) | Yes | Network transmission capabilities and bearer types of the data network. | 2730 2731## NetCapabilities 2732 2733Defines the network capability set. 2734 2735**System capability**: SystemCapability.Communication.NetManager.Core 2736 2737| Name | Type | Mandatory| Description | 2738| --------------------- | ---------------------------------- | --- | ------------------------ | 2739| linkUpBandwidthKbps | number | No| Uplink (device-to-network) bandwidth, in kbit/s. The value **0** indicates that the network bandwidth cannot be evaluated.| 2740| linkDownBandwidthKbps | number | No| Downlink (network-to-device) bandwidth, in kbit/s. The value **0** indicates that the network bandwidth cannot be evaluated.| 2741| networkCap | Array\<[NetCap](#netcap)> | No| Network capability.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 2742| 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. | 2743 2744## NetConnectionPropertyInfo<sup>11+</sup> 2745 2746Defines the network connection properties. 2747 2748**System capability**: SystemCapability.Communication.NetManager.Core 2749 2750### Attributes 2751 2752| Name | Type | Mandatory| Description | 2753| -------------------- | --------------------------------------------------- | ---- |----------------------- | 2754| netHandle | [NetHandle](#nethandle) | Yes |Data network handle.| 2755| connectionProperties | [ConnectionProperties](#connectionproperties) | Yes |Network connection properties. | 2756 2757## NetBlockStatusInfo<sup>11+</sup> 2758 2759Obtains the network block status information. 2760 2761**System capability**: SystemCapability.Communication.NetManager.Core 2762 2763### Attributes 2764 2765| Name | Type | Mandatory| Description | 2766| -------------------- | ------------------------------------- | --- |--------------------------- | 2767| netHandle | [NetHandle](#nethandle) | Yes |Data network handle. | 2768| blocked | boolean | Yes |Whether the current network is blocked.| 2769 2770## ConnectionProperties 2771 2772Defines the network connection properties. 2773 2774**System capability**: SystemCapability.Communication.NetManager.Core 2775 2776| Name | Type | Mandatory| Description | 2777| ------------- | ----------------------------------- | ----|--------------------------------------- | 2778| interfaceName | string | Yes|Network interface card (NIC) name. | 2779| domains | string | Yes|Domain name. | 2780| linkAddresses | Array\<[LinkAddress](#linkaddress)> | Yes|Link information. | 2781| routes | Array\<[RouteInfo](#routeinfo)> | Yes|Route information. | 2782| dnses | Array\<[NetAddress](#netaddress)> | Yes|Network address. For details, see [NetAddress](#netaddress).| 2783| mtu | number | Yes|Maximum transmission unit (MTU). | 2784 2785## RouteInfo 2786 2787Defines network route information. 2788 2789**System capability**: SystemCapability.Communication.NetManager.Core 2790 2791| Name | Type | Mandatory| Description | 2792| -------------- | --------------------------- | --- |-------------- | 2793| interface | string | Yes|NIC name. | 2794| destination | [LinkAddress](#linkaddress) | Yes|Destination address. | 2795| gateway | [NetAddress](#netaddress) | Yes|Gateway address. | 2796| hasGateway | boolean | Yes|Whether a gateway is present. | 2797| isDefaultRoute | boolean | Yes|Whether the route is the default route.| 2798 2799## LinkAddress 2800 2801Defines network link information. 2802 2803**System capability**: SystemCapability.Communication.NetManager.Core 2804 2805| Name | Type | Mandatory| Description | 2806| ------------ | ------------------------- |---- |-------------------- | 2807| address | [NetAddress](#netaddress) | Yes | Link address. | 2808| prefixLength | number | Yes |Length of the link address prefix. | 2809 2810## NetAddress 2811 2812Defines a network address. 2813 2814**Atomic service API**: This API can be used in atomic services since API version 12. 2815 2816**System capability**: SystemCapability.Communication.NetManager.Core 2817 2818| Name | Type |Mandatory| Description | 2819| ------- | ------ | -- |---------------------------- | 2820| address | string | Yes|Network address. | 2821| family | number | No|Address family identifier. The value is **1** for IPv4 and **2** for IPv6. The default value is **1**.| 2822| port | number | No|Port number. The value ranges from **0** to **65535**. | 2823 2824## HttpRequest 2825 2826type HttpRequest = http.HttpRequest 2827 2828Defines an HTTP request. 2829 2830**Atomic service API**: This API can be used in atomic services since API version 11. 2831 2832**System capability**: SystemCapability.Communication.NetStack 2833 2834| Type | Description | 2835| ---------------- | --------------------------- | 2836| http.HttpRequest | HTTP request task. You need to obtain an HTTP request task before calling **HttpRequest** APIs .| 2837 2838## TCPSocket 2839 2840type TCPSocket = socket.TCPSocket 2841 2842Defines a **TCPSocket** object. 2843 2844**Atomic service API**: This API can be used in atomic services since API version 10. 2845 2846**System capability**: SystemCapability.Communication.NetStack 2847 2848| Type | Description | 2849| ---------------- | --------------------------- | 2850| socket.TCPSocket | **TCPSocket** object. | 2851 2852## UDPSocket 2853 2854type UDPSocket = socket.UDPSocket 2855 2856Defines a **UDPSocket** object. 2857 2858**Atomic service API**: This API can be used in atomic services since API version 10. 2859 2860**System capability**: SystemCapability.Communication.NetStack 2861 2862| Type | Description | 2863| ---------------- | --------------------------- | 2864| socket.UDPSocket | **UDPSocket** object. | 2865