• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.net.connection (网络连接管理)
2
3网络连接管理提供管理网络一些基础能力,包括获取默认激活的数据网络、获取所有激活数据网络列表、开启关闭飞行模式、获取网络能力信息等功能。
4
5> **说明:**
6> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
7
8## 导入模块
9
10```ts
11import connection from '@ohos.net.connection';
12```
13
14## connection.createNetConnection
15
16createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection
17
18创建一个NetConnection对象,[netSpecifier](#netspecifier)指定关注的网络的各项特征;timeout是超时时间(单位是毫秒);netSpecifier是timeout的必要条件,两者都没有则表示关注默认网络。
19
20**系统能力**:SystemCapability.Communication.NetManager.Core
21
22**参数:**
23
24| 参数名       | 类型                          | 必填 | 说明                                                         |
25| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ |
26| netSpecifier | [NetSpecifier](#netspecifier) | 否   | 指定网络的各项特征,不指定或为undefined时关注默认网络。                   |
27| timeout      | number                        | 否   | 获取netSpecifier指定的网络时的超时时间,仅netSpecifier存在时生效,undefined时默认值为0。 |
28
29**返回值:**
30
31| 类型                            | 说明                 |
32| ------------------------------- | -------------------- |
33| [NetConnection](#netconnection) | 所关注的网络的句柄。 |
34
35**示例:**
36
37```ts
38import connection from '@ohos.net.connection';
39
40// 关注默认网络, 不需要传参
41let netConnection = connection.createNetConnection();
42
43// 关注蜂窝网络,需要传入相关网络特征,timeout参数未传入说明未使用超时时间,此时timeout为0
44let netConnectionCellular = connection.createNetConnection({
45  netCapabilities: {
46    bearerTypes: [connection.NetBearType.BEARER_CELLULAR]
47  }
48});
49```
50
51## connection.getDefaultNet
52
53getDefaultNet(callback: AsyncCallback\<NetHandle>): void
54
55获取默认激活的数据网络,使用callback方式作为异步方法。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。
56
57**需要权限**:ohos.permission.GET_NETWORK_INFO
58
59**系统能力**:SystemCapability.Communication.NetManager.Core
60
61**参数:**
62
63| 参数名   | 类型                                    | 必填 | 说明                                                         |
64| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
65| callback | AsyncCallback\<[NetHandle](#nethandle)> | 是   | 回调函数。当成功获取默认激活的数据网络时,error为undefined,data为默认激活的数据网络;否则为错误对象。 |
66
67**错误码:**
68
69| 错误码ID | 错误信息                        |
70| ------- | -----------------------------  |
71| 201     | Permission denied.             |
72| 401     | Parameter error.             |
73| 2100002 | Operation failed. Cannot connect to service.|
74| 2100003 | System internal error.         |
75
76**示例:**
77
78```ts
79import connection from '@ohos.net.connection';
80import { BusinessError } from '@ohos.base';
81
82connection.getDefaultNet((error: BusinessError, data: connection.NetHandle) => {
83  console.log(JSON.stringify(error));
84  console.log(JSON.stringify(data));
85});
86```
87
88## connection.getDefaultNet
89
90getDefaultNet(): Promise\<NetHandle>
91
92获取默认激活的数据网络,使用Promise方式作为异步方法。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。
93
94**需要权限**:ohos.permission.GET_NETWORK_INFO
95
96**系统能力**:SystemCapability.Communication.NetManager.Core
97
98**返回值:**
99
100| 类型                              | 说明                                  |
101| --------------------------------- | ------------------------------------- |
102| Promise\<[NetHandle](#nethandle)> | 以Promise形式返回默认激活的数据网络。 |
103
104**错误码:**
105
106| 错误码ID | 错误信息                        |
107| ------- | -----------------------------  |
108| 201     | Permission denied.             |
109| 401     | Parameter error.             |
110| 2100002 | Operation failed. Cannot connect to service.|
111| 2100003 | System internal error.         |
112
113**示例:**
114
115```ts
116import connection from '@ohos.net.connection';
117connection.getDefaultNet().then((data: connection.NetHandle) => {
118  console.log(JSON.stringify(data));
119});
120```
121
122## connection.getDefaultNetSync<sup>9+</sup>
123
124getDefaultNetSync(): NetHandle
125
126使用同步方法获取默认激活的数据网络。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。
127
128**需要权限**:ohos.permission.GET_NETWORK_INFO
129
130**系统能力**:SystemCapability.Communication.NetManager.Core
131
132**返回值:**
133
134| 类型      | 说明                               |
135| --------- | ---------------------------------- |
136| [NetHandle](#nethandle) | 以同步方式返回默认激活的数据网络。 |
137
138**错误码:**
139
140| 错误码ID | 错误信息                        |
141| ------- | -----------------------------  |
142| 201     | Permission denied.             |
143| 401     | Parameter error.             |
144| 2100002 | Operation failed. Cannot connect to service.|
145| 2100003 | System internal error.         |
146
147**示例:**
148
149```ts
150import connection from '@ohos.net.connection';
151
152let netHandle = connection.getDefaultNetSync();
153```
154
155## connection.getGlobalHttpProxy<sup>10+</sup>
156
157getGlobalHttpProxy(callback: AsyncCallback\<HttpProxy>): void
158
159获取网络的全局代理配置信息,使用callback方式作为异步方法。
160
161**系统接口**:此接口为系统接口。
162
163**系统能力**:SystemCapability.Communication.NetManager.Core
164
165**参数:**
166
167| 参数名   | 类型                                    | 必填 | 说明                                                         |
168| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
169| callback | AsyncCallback\<[HttpProxy](#httpproxy10)> | 是   | 回调函数。当成功获取网络的全局代理配置信息时,error为undefined,data为网络的全局代理配置信息;否则为错误对象 |
170
171**错误码:**
172
173| 错误码ID | 错误信息                        |
174| ------- | -----------------------------  |
175| 401     | Parameter error.             |
176| 202     | Non-system applications use system APIs.             |
177| 2100002 | Operation failed. Cannot connect to service.|
178| 2100003 | System internal error.         |
179
180**示例:**
181
182```ts
183import connection from '@ohos.net.connection';
184import { BusinessError } from '@ohos.base';
185
186connection.getGlobalHttpProxy((error: BusinessError, data: connection.HttpProxy) => {
187  console.info(JSON.stringify(error));
188  console.info(JSON.stringify(data));
189});
190```
191
192## connection.getGlobalHttpProxy<sup>10+</sup>
193
194getGlobalHttpProxy(): Promise\<HttpProxy>;
195
196获取网络的全局代理配置信息,使用Promise方式作为异步方法。
197
198**系统接口**:此接口为系统接口。
199
200**系统能力**:SystemCapability.Communication.NetManager.Core
201
202**返回值:**
203
204| 类型                              | 说明                                  |
205| --------------------------------- | ------------------------------------- |
206| Promise\<[HttpProxy](#httpproxy10)> | 以Promise形式返回网络的全局代理配置信息。 |
207
208**错误码:**
209
210| 错误码ID | 错误信息                        |
211| ------- | -----------------------------  |
212| 401     | Parameter error.             |
213| 202     | Non-system applications use system APIs.             |
214| 2100002 | Operation failed. Cannot connect to service.|
215| 2100003 | System internal error.         |
216
217**示例:**
218
219```ts
220import connection from '@ohos.net.connection';
221import { BusinessError } from '@ohos.base';
222
223connection.getGlobalHttpProxy().then((data: connection.HttpProxy) => {
224  console.info(JSON.stringify(data));
225}).catch((error: BusinessError) => {
226  console.info(JSON.stringify(error));
227});
228```
229
230## connection.setGlobalHttpProxy<sup>10+</sup>
231
232setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\<void>): void
233
234设置网络全局Http代理配置信息,使用callback方式作为异步方法。
235
236**系统接口**:此接口为系统接口。
237
238**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
239
240**系统能力**:SystemCapability.Communication.NetManager.Core
241
242**参数:**
243
244| 参数名    | 类型                    | 必填 | 说明                                                         |
245| --------- | ----------------------- | ---- | ------------------------------------------------------------ |
246| httpProxy | [HttpProxy](#httpproxy10) | 是   | 网络全局Http代理配置信息。                                  |
247| callback  | AsyncCallback\<void>    | 是   | 回调函数。当成功设置网络全局Http代理配置信息时,error为undefined,否则为错误对象。|
248
249**错误码:**
250
251| 错误码ID | 错误信息                        |
252| ------- | -----------------------------  |
253| 201     | Permission denied.             |
254| 401     | Parameter error.               |
255| 202     | Non-system applications use system APIs.               |
256| 2100001 | Invalid parameter value.                |
257| 2100002 | Operation failed. Cannot connect to service.|
258| 2100003 | System internal error.         |
259
260**示例:**
261
262```ts
263import connection from '@ohos.net.connection';
264import { BusinessError } from '@ohos.base';
265
266let exclusionStr = "192.168,baidu.com";
267let exclusionArray = exclusionStr.split(',');
268connection.setGlobalHttpProxy({
269  host: "192.168.xx.xxx",
270  port: 8080,
271  exclusionList: exclusionArray
272} as connection.HttpProxy).then(() => {
273  console.info("success");
274}).catch((error: BusinessError) => {
275  console.info(JSON.stringify(error));
276});
277```
278
279## connection.setGlobalHttpProxy<sup>10+</sup>
280
281setGlobalHttpProxy(httpProxy: HttpProxy): Promise\<void>;
282
283设置网络全局Http代理配置信息,使用Promise方式作为异步方法。
284
285**系统接口**:此接口为系统接口。
286
287**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
288
289**系统能力**:SystemCapability.Communication.NetManager.Core
290
291**参数:**
292
293| 参数名    | 类型                                                         | 必填 | 说明             |
294| --------- | ------------------------------------------------------------ | ---- | ---------------- |
295| httpProxy | [HttpProxy](#httpproxy10)                                      | 是   | 网络全局Http代理配置信息。 |
296
297**返回值:**
298
299| 类型                                        | 说明                          |
300| ------------------------------------------- | ----------------------------- |
301| Promise\<void> | 无返回值的Promise对象。 |
302
303**错误码:**
304
305| 错误码ID | 错误信息                        |
306| ------- | -----------------------------  |
307| 201     | Permission denied.             |
308| 401     | Parameter error.               |
309| 202     | Non-system applications use system APIs.               |
310| 2100001 | Invalid parameter value.                |
311| 2100002 | Operation failed. Cannot connect to service.|
312| 2100003 | System internal error.         |
313
314**示例:**
315
316```ts
317import connection from '@ohos.net.connection';
318import { BusinessError } from '@ohos.base';
319
320let exclusionStr = "192.168,baidu.com";
321let exclusionArray = exclusionStr.split(',');
322connection.setGlobalHttpProxy({
323  host: "192.168.xx.xxx",
324  port: 8080,
325  exclusionList: exclusionArray
326} as connection.HttpProxy).then(() => {
327  console.info("success");
328}).catch((error: BusinessError) => {
329  console.info(JSON.stringify(error));
330});
331```
332
333## connection.getDefaultHttpProxy<sup>10+</sup>
334
335getDefaultHttpProxy(callback: AsyncCallback\<HttpProxy>): void
336
337获取网络默认的代理配置信息。
338如果设置了全局代理,则会返回全局代理配置信息。如果进程使用[setAppNet](#connectionsetappnet9)绑定到指定[NetHandle](#nethandle)对应的网络,则返回[NetHandle](#nethandle)对应网络的代理配置信息。在其它情况下,将返回默认网络的代理配置信息。
339使用callback方式作为异步方法。
340
341**系统能力**:SystemCapability.Communication.NetManager.Core
342
343**参数:**
344
345| 参数名   | 类型                                   | 必填 | 说明                                                         |
346| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ |
347| callback | AsyncCallback<[HttpProxy](#httpproxy10)> | 是   | 回调函数。当成功获取网络默认的代理配置信息时,error为undefined,data为网络默认的代理配置信息;否则为错误对象。 |
348
349**错误码:**
350
351| 错误码ID | 错误信息                                     |
352| -------- | -------------------------------------------- |
353| 2100002  | Operation failed. Cannot connect to service. |
354| 2100003  | System internal error.                       |
355
356**示例:**
357
358```ts
359import connection from '@ohos.net.connection';
360import { BusinessError } from '@ohos.base';
361
362connection.getDefaultHttpProxy((error: BusinessError, data: connection.HttpProxy) => {
363  console.info(JSON.stringify(error));
364  console.info(JSON.stringify(data));
365});
366```
367
368## connection.getDefaultHttpProxy<sup>10+</sup>
369
370getDefaultHttpProxy(): Promise\<HttpProxy>;
371
372获取网络默认的代理配置信息。
373如果设置了全局代理,则会返回全局代理配置信息。如果进程使用[setAppNet](#connectionsetappnet9)绑定到指定[NetHandle](#nethandle)对应的网络,则返回[NetHandle](#nethandle)对应网络的代理配置信息。在其它情况下,将返回默认网络的代理配置信息。
374使用Promise方式作为异步方法。
375
376**系统能力**:SystemCapability.Communication.NetManager.Core
377
378**返回值:**
379
380| 类型                             | 说明                                      |
381| -------------------------------- | ----------------------------------------- |
382| Promise<[HttpProxy](#httpproxy10)> | 以Promise形式返回网络默认的代理配置信息。 |
383
384**错误码:**
385
386| 错误码ID | 错误信息                                     |
387| -------- | -------------------------------------------- |
388| 2100002  | Operation failed. Cannot connect to service. |
389| 2100003  | System internal error.                       |
390
391**示例:**
392
393```ts
394import connection from '@ohos.net.connection';
395import { BusinessError } from '@ohos.base';
396
397connection.getDefaultHttpProxy().then((data: connection.HttpProxy) => {
398  console.info(JSON.stringify(data));
399}).catch((error: BusinessError) => {
400  console.info(JSON.stringify(error));
401});
402```
403
404## connection.getAppNet<sup>9+</sup>
405
406getAppNet(callback: AsyncCallback\<NetHandle>): void
407
408获取App绑定的网络信息,使用callback方式作为异步方法。
409
410**系统能力**:SystemCapability.Communication.NetManager.Core
411
412**参数:**
413
414| 参数名   | 类型                                    | 必填 | 说明                                                         |
415| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
416| callback | AsyncCallback\<[NetHandle](#nethandle)> | 是   | 回调函数。当成功获取App绑定的网络信息时,error为undefined,data为获取到App绑定的网络信息;否则为错误对象。 |
417
418**错误码:**
419
420| 错误码ID | 错误信息                        |
421| ------- | -----------------------------  |
422| 401 | Parameter error.|
423| 2100002 | Operation failed. Cannot connect to service.|
424| 2100003 | System internal error.         |
425
426**示例:**
427
428```ts
429import connection from '@ohos.net.connection';
430import { BusinessError } from '@ohos.base';
431
432connection.getAppNet((error: BusinessError, data: connection.NetHandle) => {
433  console.log(JSON.stringify(error));
434  console.log(JSON.stringify(data));
435})
436```
437
438## connection.getAppNet<sup>9+</sup>
439
440getAppNet(): Promise\<NetHandle>;
441
442获取App绑定的网络信息,使用Promise方式作为异步方法。
443
444**系统能力**:SystemCapability.Communication.NetManager.Core
445
446**返回值:**
447
448| 类型                              | 说明                                  |
449| --------------------------------- | ------------------------------------- |
450| Promise\<[NetHandle](#nethandle)> | 以Promise形式返回App绑定的网络信息。 |
451
452**错误码:**
453
454| 错误码ID | 错误信息                        |
455| ------- | -----------------------------  |
456| 401 | Parameter error.|
457| 2100002 | Operation failed. Cannot connect to service.|
458| 2100003 | System internal error.         |
459
460**示例:**
461
462```ts
463import connection from '@ohos.net.connection';
464import { BusinessError } from '@ohos.base';
465
466connection.getAppNet().then((data: connection.NetHandle) => {
467  console.info(JSON.stringify(data));
468}).catch((error: BusinessError) => {
469  console.info(JSON.stringify(error));
470});
471```
472
473## connection.getAppNetSync<sup>10+</sup>
474
475getAppNetSync(): NetHandle
476
477使用同步方法获取App绑定的网络信息。
478
479**系统能力**:SystemCapability.Communication.NetManager.Core
480
481**返回值:**
482
483| 类型      | 说明                               |
484| --------- | ---------------------------------- |
485| [NetHandle](#nethandle) | 返回APP绑定的数据网络。 |
486
487**错误码:**
488
489| 错误码ID | 错误信息                        |
490| ------- | -----------------------------  |
491| 401     | Parameter error.             |
492| 2100002 | Operation failed. Cannot connect to service.|
493| 2100003 | System internal error.         |
494
495**示例:**
496
497```ts
498import connection from '@ohos.net.connection';
499
500let netHandle = connection.getAppNetSync();
501```
502
503## connection.setAppNet<sup>9+</sup>
504
505setAppNet(netHandle: NetHandle, callback: AsyncCallback\<void>): void
506
507绑定App到指定网络,绑定后的App只能通过指定网络访问外网,使用callback方式作为异步方法。
508
509**需要权限**:ohos.permission.INTERNET
510
511**系统能力**:SystemCapability.Communication.NetManager.Core
512
513**参数:**
514
515| 参数名    | 类型                    | 必填 | 说明                                                         |
516| --------- | ----------------------- | ---- | ------------------------------------------------------------ |
517| netHandle | [NetHandle](#nethandle) | 是   | 数据网络的句柄。                                             |
518| callback  | AsyncCallback\<void>    | 是   | 回调函数。当成功绑定App到指定网络时,error为undefined,否则为错误对象。|
519
520**错误码:**
521
522| 错误码ID | 错误信息                        |
523| ------- | -----------------------------  |
524| 201     | Permission denied.             |
525| 401     | Parameter error.               |
526| 2100001 | Invalid parameter value.                |
527| 2100002 | Operation failed. Cannot connect to service.|
528| 2100003 | System internal error.         |
529
530**示例:**
531
532```ts
533import connection from '@ohos.net.connection';
534import { BusinessError } from '@ohos.base';
535
536connection.getDefaultNet((error: BusinessError, netHandle: connection.NetHandle) => {
537  connection.setAppNet(netHandle, (error: BusinessError, data: void) => {
538    console.log(JSON.stringify(error));
539    console.log(JSON.stringify(data));
540  });
541});
542```
543
544## connection.setAppNet<sup>9+</sup>
545
546setAppNet(netHandle: NetHandle): Promise\<void>;
547
548绑定App到指定网络,绑定后的App只能通过指定网络访问外网,使用Promise方式作为异步方法。
549
550**需要权限**:ohos.permission.INTERNET
551
552**系统能力**:SystemCapability.Communication.NetManager.Core
553
554**参数:**
555
556| 参数名    | 类型                                                         | 必填 | 说明             |
557| --------- | ------------------------------------------------------------ | ---- | ---------------- |
558| netHandle | [NetHandle](#nethandle)                                      | 是   | 数据网络的句柄。 |
559
560**返回值:**
561
562| 类型                                        | 说明                          |
563| ------------------------------------------- | ----------------------------- |
564| Promise\<void> | 无返回值的Promise对象。 |
565
566**错误码:**
567
568| 错误码ID | 错误信息                        |
569| ------- | -----------------------------  |
570| 201     | Permission denied.             |
571| 401     | Parameter error.               |
572| 2100001 | Invalid parameter value.                |
573| 2100002 | Operation failed. Cannot connect to service.|
574| 2100003 | System internal error.         |
575
576**示例:**
577
578```ts
579import connection from '@ohos.net.connection';
580import { BusinessError } from '@ohos.base';
581
582connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
583  connection.setAppNet(netHandle).then(() => {
584    console.log("success");
585  }).catch((error: BusinessError) => {
586    console.log(JSON.stringify(error));
587  })
588});
589```
590
591## connection.getAllNets
592
593getAllNets(callback: AsyncCallback&lt;Array&lt;NetHandle&gt;&gt;): void
594
595获取所有处于连接状态的网络列表,使用callback方式作为异步方法。
596
597**需要权限**:ohos.permission.GET_NETWORK_INFO
598
599**系统能力**:SystemCapability.Communication.NetManager.Core
600
601**参数:**
602
603| 参数名 | 类型 | 必填 | 说明 |
604| -------- | -------- | -------- | -------- |
605| callback | AsyncCallback&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | 是 | 回调函数。当成功获取所有处于连接状态的网络列表时,error为undefined,data为激活的数据网络列表;否则为错误对象。|
606
607**错误码:**
608
609| 错误码ID | 错误信息                        |
610| ------- | -----------------------------  |
611| 201     | Permission denied.             |
612| 401     | Parameter error.             |
613| 2100002 | Operation failed. Cannot connect to service.|
614| 2100003 | System internal error.         |
615
616**示例:**
617
618```ts
619import connection from '@ohos.net.connection';
620import { BusinessError } from '@ohos.base';
621
622connection.getAllNets((error: BusinessError, data: connection.NetHandle[]) => {
623  console.log(JSON.stringify(error));
624  console.log(JSON.stringify(data));
625});
626```
627
628## connection.getAllNets
629
630getAllNets(): Promise&lt;Array&lt;NetHandle&gt;&gt;
631
632获取所有处于连接状态的网络列表,使用Promise方式作为异步方法。
633
634**需要权限**:ohos.permission.GET_NETWORK_INFO
635
636**系统能力**:SystemCapability.Communication.NetManager.Core
637
638**返回值:**
639
640| 类型 | 说明 |
641| -------- | -------- |
642| Promise&lt;Array&lt;[NetHandle](#nethandle)&gt;&gt; | 以Promise形式返回激活的数据网络列表。 |
643
644**错误码:**
645
646| 错误码ID | 错误信息                        |
647| ------- | -----------------------------  |
648| 201     | Permission denied.             |
649| 401     | Parameter error.             |
650| 2100002 | Operation failed. Cannot connect to service.|
651| 2100003 | System internal error.         |
652
653**示例:**
654
655```ts
656import connection from '@ohos.net.connection';
657
658connection.getAllNets().then((data: connection.NetHandle[]) => {
659  console.log(JSON.stringify(data));
660});
661```
662
663## connection.getAllNetsSync<sup>10+</sup>
664
665getAllNetsSync(): Array&lt;NetHandle&gt;
666
667使用同步方法获取所有处于连接状态的网络列表。
668
669**需要权限**:ohos.permission.GET_NETWORK_INFO
670
671**系统能力**:SystemCapability.Communication.NetManager.Core
672
673**返回值:**
674
675| 类型      | 说明                               |
676| --------- | ---------------------------------- |
677| Array&lt;[NetHandle](#nethandle)&gt; | 返回激活的数据网络列表。 |
678
679**错误码:**
680
681| 错误码ID | 错误信息                        |
682| ------- | -----------------------------  |
683| 201     | Permission denied.             |
684| 401     | Parameter error.             |
685| 2100002 | Operation failed. Cannot connect to service.|
686| 2100003 | System internal error.         |
687
688**示例:**
689
690```ts
691import connection from '@ohos.net.connection';
692
693let netHandle = connection.getAllNetsSync();
694```
695
696## connection.getConnectionProperties
697
698getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<ConnectionProperties>): void
699
700获取netHandle对应的网络的连接信息,使用callback方式作为异步方法。
701
702**需要权限**:ohos.permission.GET_NETWORK_INFO
703
704**系统能力**:SystemCapability.Communication.NetManager.Core
705
706**参数:**
707
708| 参数名    | 类型                                                         | 必填 | 说明                                                         |
709| --------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
710| netHandle | [NetHandle](#nethandle)                                      | 是   | 数据网络的句柄。                                             |
711| callback  | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | 是   | 回调函数。当成功获取netHandle对应的网络的连接信息时,error为undefined,data为获取的网络连接信息;否则为错误对象。|
712
713**错误码:**
714
715| 错误码ID | 错误信息                        |
716| ------- | -----------------------------  |
717| 201     | Permission denied.             |
718| 401     | Parameter error.               |
719| 2100001 | Invalid parameter value.                |
720| 2100002 | Operation failed. Cannot connect to service.|
721| 2100003 | System internal error.         |
722
723**示例:**
724
725```ts
726import connection from '@ohos.net.connection';
727import { BusinessError } from '@ohos.base';
728
729connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
730  connection.getConnectionProperties(netHandle, (error: BusinessError, data: connection.ConnectionProperties) => {
731    console.log(JSON.stringify(error));
732    console.log(JSON.stringify(data));
733  })
734});
735```
736
737## connection.getConnectionProperties
738
739getConnectionProperties(netHandle: NetHandle): Promise\<ConnectionProperties>
740
741获取netHandle对应的网络的连接信息,使用Promise方式作为异步方法。
742
743**需要权限**:ohos.permission.GET_NETWORK_INFO
744
745**系统能力**:SystemCapability.Communication.NetManager.Core
746
747**参数:**
748
749| 参数名    | 类型                    | 必填 | 说明             |
750| --------- | ----------------------- | ---- | ---------------- |
751| netHandle | [NetHandle](#nethandle) | 是   | 数据网络的句柄。 |
752
753**返回值:**
754
755| 类型                                                    | 说明                              |
756| ------------------------------------------------------- | --------------------------------- |
757| Promise\<[ConnectionProperties](#connectionproperties)> | 以Promise形式返回网络的连接信息。 |
758
759**错误码:**
760
761| 错误码ID | 错误信息                        |
762| ------- | -----------------------------  |
763| 201     | Permission denied.             |
764| 401     | Parameter error.               |
765| 2100001 | Invalid parameter value.                |
766| 2100002 | Operation failed. Cannot connect to service.|
767| 2100003 | System internal error.         |
768
769**示例:**
770
771```ts
772import connection from '@ohos.net.connection';
773
774connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
775  connection.getConnectionProperties(netHandle).then((data: connection.ConnectionProperties) => {
776    console.log(JSON.stringify(data));
777  })
778});
779```
780
781## connection.getConnectionPropertiesSync<sup>10+</sup>
782
783getConnectionPropertiesSync(netHandle: NetHandle): ConnectionProperties
784
785获取netHandle对应的网络的连接信息,使用同步方法返回。
786
787**需要权限**:ohos.permission.GET_NETWORK_INFO
788
789**系统能力**:SystemCapability.Communication.NetManager.Core
790
791**参数:**
792
793| 参数名    | 类型                    | 必填 | 说明             |
794| --------- | ----------------------- | ---- | ---------------- |
795| netHandle | [NetHandle](#nethandle) | 是   | 数据网络的句柄。 |
796
797**返回值:**
798
799| 类型                                                    | 说明                              |
800| ------------------------------------------------------- | --------------------------------- |
801| [ConnectionProperties](#connectionproperties) | 返回网络的连接信息。 |
802
803**错误码:**
804
805| 错误码ID | 错误信息                        |
806| ------- | -----------------------------  |
807| 201     | Permission denied.             |
808| 401     | Parameter error.               |
809| 2100001 | Invalid parameter value.                |
810| 2100002 | Operation failed. Cannot connect to service.|
811| 2100003 | System internal error.         |
812
813**示例:**
814
815```ts
816import connection from '@ohos.net.connection';
817
818let netHandle = connection.getDefaultNetSync();
819let connectionproperties = connection.getConnectionPropertiesSync(netHandle);
820```
821
822## connection.getNetCapabilities
823
824getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\<NetCapabilities>): void
825
826获取netHandle对应的网络的能力信息,使用callback方式作为异步方法。
827
828**需要权限**:ohos.permission.GET_NETWORK_INFO
829
830**系统能力**:SystemCapability.Communication.NetManager.Core
831
832**参数:**
833
834| 参数名    | 类型                                                | 必填 | 说明                                                         |
835| --------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ |
836| netHandle | [NetHandle](#nethandle)                             | 是   | 数据网络的句柄。                                             |
837| callback  | AsyncCallback\<[NetCapabilities](#netcapabilities)> | 是   | 回调函数。当成功获取netHandle对应的网络的能力信息时,error为undefined,data为获取到的网络能力信息;否则为错误对象。|
838
839**错误码:**
840
841| 错误码ID | 错误信息                        |
842| ------- | -----------------------------  |
843| 201     | Permission denied.             |
844| 401     | Parameter error.               |
845| 2100001 | Invalid parameter value.                |
846| 2100002 | Operation failed. Cannot connect to service.|
847| 2100003 | System internal error.         |
848
849**示例:**
850
851```ts
852import connection from '@ohos.net.connection';
853import { BusinessError } from '@ohos.base';
854
855connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
856  connection.getNetCapabilities(netHandle, (error: BusinessError, data: connection.NetCapabilities) => {
857    console.log(JSON.stringify(error));
858    console.log(JSON.stringify(data));
859  })
860});
861```
862
863## connection.getNetCapabilities
864
865getNetCapabilities(netHandle: NetHandle): Promise\<NetCapabilities>
866
867获取netHandle对应的网络的能力信息,使用Promise方式作为异步方法。
868
869**需要权限**:ohos.permission.GET_NETWORK_INFO
870
871**系统能力**:SystemCapability.Communication.NetManager.Core
872
873**参数:**
874
875| 参数名    | 类型                    | 必填 | 说明             |
876| --------- | ----------------------- | ---- | ---------------- |
877| netHandle | [NetHandle](#nethandle) | 是   | 数据网络的句柄。 |
878
879**返回值:**
880
881| 类型                                          | 说明                              |
882| --------------------------------------------- | --------------------------------- |
883| Promise\<[NetCapabilities](#netcapabilities)> | 以Promise形式返回网络的能力信息。 |
884
885**错误码:**
886
887| 错误码ID | 错误信息                        |
888| ------- | -----------------------------  |
889| 201     | Permission denied.             |
890| 401     | Parameter error.               |
891| 2100001 | Invalid parameter value.                |
892| 2100002 | Operation failed. Cannot connect to service.|
893| 2100003 | System internal error.         |
894
895**示例:**
896
897```ts
898import connection from '@ohos.net.connection';
899
900connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
901  connection.getNetCapabilities(netHandle).then((data: connection.NetCapabilities) => {
902    console.log(JSON.stringify(data));
903  })
904});
905```
906
907## connection.getNetCapabilitiesSync<sup>10+</sup>
908
909getNetCapabilitiesSync(netHandle: NetHandle): NetCapabilities
910
911获取netHandle对应的网络的能力信息,使用同步方式返回。
912
913**需要权限**:ohos.permission.GET_NETWORK_INFO
914
915**系统能力**:SystemCapability.Communication.NetManager.Core
916
917**参数:**
918
919| 参数名    | 类型                    | 必填 | 说明             |
920| --------- | ----------------------- | ---- | ---------------- |
921| netHandle | [NetHandle](#nethandle) | 是   | 数据网络的句柄。 |
922
923**返回值:**
924
925| 类型                                          | 说明                              |
926| --------------------------------------------- | --------------------------------- |
927| [NetCapabilities](#netcapabilities) | 返回网络的能力信息。 |
928
929**错误码:**
930
931| 错误码ID | 错误信息                        |
932| ------- | -----------------------------  |
933| 201     | Permission denied.             |
934| 401     | Parameter error.               |
935| 2100001 | Invalid parameter value.                |
936| 2100002 | Operation failed. Cannot connect to service.|
937| 2100003 | System internal error.         |
938
939**示例:**
940
941```ts
942import connection from '@ohos.net.connection';
943
944let netHandle = connection.getDefaultNetSync();
945let getNetCapabilitiesSync = connection.getNetCapabilitiesSync(netHandle);
946```
947
948## connection.isDefaultNetMetered<sup>9+</sup>
949
950isDefaultNetMetered(callback: AsyncCallback\<boolean>): void
951
952检查当前网络上的数据流量使用是否被计量,使用callback方式作为异步方法。
953
954**需要权限**:ohos.permission.GET_NETWORK_INFO
955
956**系统能力**:SystemCapability.Communication.NetManager.Core
957
958**参数:**
959
960| 参数名   | 类型                    | 必填 | 说明                                   |
961| -------- | ----------------------- | ---- | -------------------------------------- |
962| callback | AsyncCallback\<boolean> | 是   | 回调函数。当前网络上的数据流量使用被计量返回true。 |
963
964**错误码:**
965
966| 错误码ID | 错误信息                        |
967| ------- | -----------------------------  |
968| 201     | Permission denied.             |
969| 401     | Parameter error.               |
970| 2100002 | Operation failed. Cannot connect to service.|
971| 2100003 | System internal error.         |
972
973**示例:**
974
975```ts
976import connection from '@ohos.net.connection';
977import { BusinessError } from '@ohos.base';
978
979connection.isDefaultNetMetered((error: BusinessError, data: boolean) => {
980  console.log(JSON.stringify(error));
981  console.log('data: ' + data);
982});
983```
984
985## connection.isDefaultNetMetered<sup>9+</sup>
986
987isDefaultNetMetered(): Promise\<boolean>
988
989检查当前网络上的数据流量使用是否被计量,使用Promise方式作为异步方法。
990
991**需要权限**:ohos.permission.GET_NETWORK_INFO
992
993**系统能力**:SystemCapability.Communication.NetManager.Core
994
995**返回值:**
996
997| 类型              | 说明                                            |
998| ----------------- | ----------------------------------------------- |
999| Promise\<boolean> | 以Promise形式返回,当前网络上的数据流量使用被计量true。 |
1000
1001**错误码:**
1002
1003| 错误码ID | 错误信息                        |
1004| ------- | -----------------------------  |
1005| 201     | Permission denied.             |
1006| 401     | Parameter error.               |
1007| 2100002 | Operation failed. Cannot connect to service.|
1008| 2100003 | System internal error.         |
1009
1010**示例:**
1011
1012```ts
1013import connection from '@ohos.net.connection';
1014
1015connection.isDefaultNetMetered().then((data: boolean) => {
1016  console.log('data: ' + data);
1017});
1018```
1019
1020## connection.isDefaultNetMeteredSync<sup>10+</sup>
1021
1022isDefaultNetMeteredSync(): boolean
1023
1024检查当前网络上的数据流量使用是否被计量,使用同步方式返回。
1025
1026**需要权限**:ohos.permission.GET_NETWORK_INFO
1027
1028**系统能力**:SystemCapability.Communication.NetManager.Core
1029
1030**返回值:**
1031
1032| 类型              | 说明                                            |
1033| ----------------- | ----------------------------------------------- |
1034| boolean | 当前网络上的数据流量使用被计量true。 |
1035
1036**错误码:**
1037
1038| 错误码ID | 错误信息                        |
1039| ------- | -----------------------------  |
1040| 201     | Permission denied.             |
1041| 401     | Parameter error.               |
1042| 2100002 | Operation failed. Cannot connect to service.|
1043| 2100003 | System internal error.         |
1044
1045**示例:**
1046
1047```ts
1048import connection from '@ohos.net.connection';
1049
1050let isMetered = connection.isDefaultNetMeteredSync();
1051```
1052
1053## connection.hasDefaultNet
1054
1055hasDefaultNet(callback: AsyncCallback\<boolean>): void
1056
1057检查默认数据网络是否被激活,使用callback方式作为异步方法。如果有默认数据网路,可以使用[getDefaultNet](#connectiongetdefaultnet)去获取。
1058
1059**需要权限**:ohos.permission.GET_NETWORK_INFO
1060
1061**系统能力**:SystemCapability.Communication.NetManager.Core
1062
1063**参数:**
1064
1065| 参数名   | 类型                    | 必填 | 说明                                   |
1066| -------- | ----------------------- | ---- | -------------------------------------- |
1067| callback | AsyncCallback\<boolean> | 是   | 回调函数。默认数据网络被激活返回true。 |
1068
1069**错误码:**
1070
1071| 错误码ID | 错误信息                        |
1072| ------- | -----------------------------  |
1073| 201     | Permission denied.             |
1074| 401     | Parameter error.               |
1075| 2100002 | Operation failed. Cannot connect to service.|
1076| 2100003 | System internal error.         |
1077
1078**示例:**
1079
1080```ts
1081import connection from '@ohos.net.connection';
1082import { BusinessError } from '@ohos.base';
1083
1084connection.hasDefaultNet((error: BusinessError, data: boolean) => {
1085  console.log(JSON.stringify(error));
1086  console.log('data: ' + data);
1087});
1088```
1089
1090## connection.hasDefaultNet
1091
1092hasDefaultNet(): Promise\<boolean>
1093
1094检查默认数据网络是否被激活,使用Promise方式作为异步方法。如果有默认数据网路,可以使用[getDefaultNet](#connectiongetdefaultnet)去获取。
1095
1096**需要权限**:ohos.permission.GET_NETWORK_INFO
1097
1098**系统能力**:SystemCapability.Communication.NetManager.Core
1099
1100**返回值:**
1101
1102| 类型              | 说明                                            |
1103| ----------------- | ----------------------------------------------- |
1104| Promise\<boolean> | 以Promise形式返回,默认数据网络被激活返回true。 |
1105
1106**错误码:**
1107
1108| 错误码ID | 错误信息                        |
1109| ------- | -----------------------------  |
1110| 201     | Permission denied.             |
1111| 401     | Parameter error.               |
1112| 2100002 | Operation failed. Cannot connect to service.|
1113| 2100003 | System internal error.         |
1114
1115**示例:**
1116
1117```ts
1118import connection from '@ohos.net.connection';
1119connection.hasDefaultNet().then((data: boolean) => {
1120  console.log('data: ' + data);
1121});
1122```
1123
1124## connection.hasDefaultNetSync<sup>10+</sup>
1125
1126hasDefaultNetSync(): boolean
1127
1128检查默认数据网络是否被激活,使用同步方式返回接口,如果被激活则返回true。
1129
1130**需要权限**:ohos.permission.GET_NETWORK_INFO
1131
1132**系统能力**:SystemCapability.Communication.NetManager.Core
1133
1134**返回值:**
1135
1136| 类型              | 说明                                            |
1137| ----------------- | ----------------------------------------------- |
1138| boolean | 默认数据网络被激活返回true。 |
1139
1140**错误码:**
1141
1142| 错误码ID | 错误信息                        |
1143| ------- | -----------------------------  |
1144| 201     | Permission denied.             |
1145| 401     | Parameter error.               |
1146| 2100002 | Operation failed. Cannot connect to service.|
1147| 2100003 | System internal error.         |
1148
1149**示例:**
1150
1151```ts
1152import connection from '@ohos.net.connection';
1153
1154let isDefaultNet = connection.hasDefaultNetSync();
1155```
1156
1157## connection.enableAirplaneMode
1158
1159enableAirplaneMode(callback: AsyncCallback\<void>): void
1160
1161开启飞行模式,使用callback方式作为异步方法。
1162
1163**系统接口**:此接口为系统接口。
1164
1165**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
1166
1167**系统能力**:SystemCapability.Communication.NetManager.Core
1168
1169**参数:**
1170
1171| 参数名   | 类型                                              | 必填 | 说明               |
1172| -------- | ------------------------------------------------- | ---- | ------------------ |
1173| callback | AsyncCallback\<void> | 是   | 回调函数。         |
1174
1175**错误码:**
1176
1177| 错误码ID | 错误信息                        |
1178| ------- | -----------------------------  |
1179| 201     | Permission denied.             |
1180| 202     | Non-system applications use system APIs.              |
1181| 401     | Parameter error.               |
1182| 2100002 | Operation failed. Cannot connect to service.|
1183| 2100003 | System internal error.         |
1184
1185**示例:**
1186
1187```ts
1188import connection from '@ohos.net.connection';
1189import { BusinessError } from '@ohos.base';
1190
1191connection.enableAirplaneMode((error: BusinessError) => {
1192  console.log(JSON.stringify(error));
1193});
1194```
1195
1196## connection.enableAirplaneMode
1197
1198enableAirplaneMode(): Promise\<void>
1199
1200开启飞行模式,使用Promise方式作为异步方法。
1201
1202**系统接口**:此接口为系统接口。
1203
1204**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
1205
1206**系统能力**:SystemCapability.Communication.NetManager.Core
1207
1208**返回值:**
1209
1210| 类型                                        | 说明                          |
1211| ------------------------------------------- | ----------------------------- |
1212| Promise\<void> | 无返回值的Promise对象。 |
1213
1214**错误码:**
1215
1216| 错误码ID | 错误信息                        |
1217| ------- | -----------------------------  |
1218| 201     | Permission denied.             |
1219| 202     | Non-system applications use system APIs.              |
1220| 401     | Parameter error.               |
1221| 2100002 | Operation failed. Cannot connect to service.|
1222| 2100003 | System internal error.         |
1223
1224**示例:**
1225
1226```ts
1227import connection from '@ohos.net.connection';
1228
1229connection.enableAirplaneMode().then((error: void) => {
1230  console.log(JSON.stringify(error));
1231});
1232```
1233
1234## connection.disableAirplaneMode
1235
1236disableAirplaneMode(callback: AsyncCallback\<void>): void
1237
1238关闭飞行模式,使用callback方式作为异步方法。
1239
1240**系统接口**:此接口为系统接口。
1241
1242**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
1243
1244**系统能力**:SystemCapability.Communication.NetManager.Core
1245
1246**参数:**
1247
1248| 参数名   | 类型                                              | 必填 | 说明               |
1249| -------- | ------------------------------------------------- | ---- | ------------------ |
1250| callback | AsyncCallback\<void> | 是   | 回调函数。当关闭飞行模式成功,error为undefined,否则为错误对象。 |
1251
1252**错误码:**
1253
1254| 错误码ID | 错误信息                        |
1255| ------- | -----------------------------  |
1256| 201     | Permission denied.             |
1257| 202     | Non-system applications use system APIs.              |
1258| 401     | Parameter error.               |
1259| 2100002 | Operation failed. Cannot connect to service.|
1260| 2100003 | System internal error.         |
1261
1262**示例:**
1263
1264```ts
1265import connection from '@ohos.net.connection';
1266import { BusinessError } from '@ohos.base';
1267
1268connection.disableAirplaneMode((error: BusinessError) => {
1269  console.log(JSON.stringify(error));
1270});
1271```
1272
1273## connection.disableAirplaneMode
1274
1275disableAirplaneMode(): Promise\<void>
1276
1277关闭飞行模式,使用Promise方式作为异步方法。
1278
1279**系统接口**:此接口为系统接口。
1280
1281**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
1282
1283**系统能力**:SystemCapability.Communication.NetManager.Core
1284
1285**返回值:**
1286
1287| 类型                                        | 说明                          |
1288| ------------------------------------------- | ----------------------------- |
1289| Promise\<void> | 无返回值的Promise对象。 |
1290
1291**错误码:**
1292
1293| 错误码ID | 错误信息                        |
1294| ------- | -----------------------------  |
1295| 201     | Permission denied.             |
1296| 202     | Non-system applications use system APIs.              |
1297| 401     | Parameter error.               |
1298| 2100002 | Operation failed. Cannot connect to service.|
1299| 2100003 | System internal error.         |
1300
1301**示例:**
1302
1303```ts
1304import connection from '@ohos.net.connection';
1305
1306connection.disableAirplaneMode().then((error: void) => {
1307  console.log(JSON.stringify(error));
1308});
1309```
1310
1311## connection.reportNetConnected
1312
1313reportNetConnected(netHandle: NetHandle, callback: AsyncCallback&lt;void&gt;): void
1314
1315向网络管理报告网络处于可用状态,使用callback方式作为异步方法。
1316
1317**需要权限**:ohos.permission.GET_NETWORK_INFOohos.permission.INTERNET
1318
1319**系统能力**:SystemCapability.Communication.NetManager.Core
1320
1321**参数:**
1322
1323| 参数名 | 类型 | 必填 | 说明 |
1324| -------- | -------- | -------- | -------- |
1325| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
1326| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当向网络管理报告网络处于可用状态成功,error为undefined,否则为错误对象。 |
1327
1328**错误码:**
1329
1330| 错误码ID | 错误信息                        |
1331| ------- | -----------------------------  |
1332| 201     | Permission denied.             |
1333| 401     | Parameter error.               |
1334| 2100001 | Invalid parameter value.                |
1335| 2100002 | Operation failed. Cannot connect to service.|
1336| 2100003 | System internal error.         |
1337
1338**示例:**
1339
1340```ts
1341import connection from '@ohos.net.connection';
1342import { BusinessError } from '@ohos.base';
1343
1344connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
1345  connection.reportNetConnected(netHandle, (error: BusinessError) => {
1346    console.log(JSON.stringify(error));
1347  });
1348});
1349```
1350
1351## connection.reportNetConnected
1352
1353reportNetConnected(netHandle: NetHandle): Promise&lt;void&gt;
1354
1355向网络管理报告网络处于可用状态,使用Promise方式作为异步方法。
1356
1357**需要权限**:ohos.permission.GET_NETWORK_INFOohos.permission.INTERNET
1358
1359**系统能力**:SystemCapability.Communication.NetManager.Core
1360
1361**参数:**
1362
1363| 参数名 | 类型 | 必填 | 说明 |
1364| -------- | -------- | -------- | -------- |
1365| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
1366
1367**返回值:**
1368| 类型 | 说明 |
1369| -------- | -------- |
1370| Promise&lt;void&gt; | 无返回值的Promise对象。 |
1371
1372**错误码:**
1373
1374| 错误码ID | 错误信息                        |
1375| ------- | -----------------------------  |
1376| 201     | Permission denied.             |
1377| 401     | Parameter error.               |
1378| 2100001 | Invalid parameter value.                |
1379| 2100002 | Operation failed. Cannot connect to service.|
1380| 2100003 | System internal error.         |
1381
1382**示例:**
1383
1384```ts
1385import connection from '@ohos.net.connection';
1386connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
1387  connection.reportNetConnected(netHandle).then(() => {
1388    console.log(`report success`);
1389  });
1390});
1391```
1392
1393## connection.reportNetDisconnected
1394
1395reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback&lt;void&gt;): void
1396
1397向网络管理报告网络处于不可用状态,使用callback方式作为异步方法。
1398
1399**需要权限**:ohos.permission.GET_NETWORK_INFOohos.permission.INTERNET
1400
1401**系统能力**:SystemCapability.Communication.NetManager.Core
1402
1403**参数:**
1404
1405| 参数名 | 类型 | 必填 | 说明 |
1406| -------- | -------- | -------- | -------- |
1407| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
1408| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数。当向网络管理报告网络处于不可用状态成功,error为undefined,否则为错误对象。 |
1409
1410**错误码:**
1411
1412| 错误码ID | 错误信息                        |
1413| ------- | -----------------------------  |
1414| 201     | Permission denied.             |
1415| 401     | Parameter error.               |
1416| 2100001 | Invalid parameter value.                |
1417| 2100002 | Operation failed. Cannot connect to service.|
1418| 2100003 | System internal error.         |
1419
1420**示例:**
1421
1422```ts
1423import connection from '@ohos.net.connection';
1424connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
1425  connection.reportNetDisconnected(netHandle).then( () => {
1426    console.log(`report success`);
1427  });
1428});
1429```
1430
1431## connection.reportNetDisconnected
1432
1433reportNetDisconnected(netHandle: NetHandle): Promise&lt;void&gt;
1434
1435向网络管理报告网络处于不可用状态,使用Promise方式作为异步方法。
1436
1437**需要权限**:ohos.permission.GET_NETWORK_INFOohos.permission.INTERNET
1438
1439**系统能力**:SystemCapability.Communication.NetManager.Core
1440
1441**参数:**
1442
1443| 参数名 | 类型 | 必填 | 说明 |
1444| -------- | -------- | -------- | -------- |
1445| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 |
1446
1447**返回值:**
1448| 类型 | 说明 |
1449| -------- | -------- |
1450| Promise&lt;void&gt; | 无返回值的Promise对象。 |
1451
1452**错误码:**
1453
1454| 错误码ID | 错误信息                        |
1455| ------- | -----------------------------  |
1456| 201     | Permission denied.             |
1457| 401     | Parameter error.               |
1458| 2100001 | Invalid parameter value.                |
1459| 2100002 | Operation failed. Cannot connect to service.|
1460| 2100003 | System internal error.         |
1461
1462**示例:**
1463
1464```ts
1465import connection from '@ohos.net.connection';
1466connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
1467  connection.reportNetDisconnected(netHandle).then( () => {
1468    console.log(`report success`);
1469  });
1470});
1471```
1472
1473## connection.getAddressesByName
1474
1475getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void
1476
1477使用对应网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。
1478
1479**需要权限**:ohos.permission.INTERNET
1480
1481**系统能力**:SystemCapability.Communication.NetManager.Core
1482
1483**参数:**
1484
1485| 参数名   | 类型                                              | 必填 | 说明                                                         |
1486| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ |
1487| host     | string                                            | 是   | 需要解析的主机名。                                           |
1488| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是   | 回调函数。当使用默认网络解析主机名成功获取所有IP地址,error为undefined,data为获取到的所有IP地址;否则为错误对象。 |
1489
1490**错误码:**
1491
1492| 错误码ID | 错误信息                        |
1493| ------- | -----------------------------  |
1494| 201     | Permission denied.             |
1495| 401     | Parameter error.               |
1496| 2100001 | Invalid parameter value.                |
1497| 2100002 | Operation failed. Cannot connect to service.|
1498| 2100003 | System internal error.         |
1499
1500**示例:**
1501
1502```ts
1503import connection from '@ohos.net.connection';
1504import { BusinessError } from "@ohos.base";
1505connection.getAddressesByName("xxxx", (error: BusinessError, data: connection.NetAddress[]) => {
1506  console.log(JSON.stringify(error));
1507  console.log(JSON.stringify(data));
1508});
1509```
1510
1511## connection.getAddressesByName
1512
1513getAddressesByName(host: string): Promise\<Array\<NetAddress>>
1514
1515使用对应网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。
1516
1517**需要权限**:ohos.permission.INTERNET
1518
1519**系统能力**:SystemCapability.Communication.NetManager.Core
1520
1521**参数:**
1522
1523| 参数名 | 类型   | 必填 | 说明               |
1524| ------ | ------ | ---- | ------------------ |
1525| host   | string | 是   | 需要解析的主机名。 |
1526
1527**返回值:**
1528
1529| 类型                                        | 说明                          |
1530| ------------------------------------------- | ----------------------------- |
1531| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回所有IP地址。 |
1532
1533**错误码:**
1534
1535| 错误码ID | 错误信息                        |
1536| ------- | -----------------------------  |
1537| 201     | Permission denied.             |
1538| 401     | Parameter error.               |
1539| 2100001 | Invalid parameter value.                |
1540| 2100002 | Operation failed. Cannot connect to service.|
1541| 2100003 | System internal error.         |
1542
1543**示例:**
1544
1545```ts
1546import connection from '@ohos.net.connection';
1547connection.getAddressesByName("xxxx").then((data: connection.NetAddress[]) => {
1548  console.log(JSON.stringify(data));
1549});
1550```
1551
1552## NetConnection
1553
1554网络连接的句柄。
1555
1556> **说明:**
1557> 设备从无网络到有网络会触发netAvailable事件、netCapabilitiesChange事件和netConnectionPropertiesChange事件;
1558> 设备从有网络到无网络状态会触发netLost事件;
1559> 设备从WiFi到蜂窝会触发netLost事件(WiFi丢失)之后触发 netAvaliable事件(蜂窝可用);
1560
1561### register
1562
1563register(callback: AsyncCallback\<void>): void
1564
1565订阅指定网络状态变化的通知。
1566
1567**需要权限**:ohos.permission.GET_NETWORK_INFO
1568
1569**系统能力**:SystemCapability.Communication.NetManager.Core
1570
1571**参数:**
1572
1573| 参数名   | 类型                 | 必填 | 说明                                                         |
1574| -------- | -------------------- | ---- | ------------------------------------------------------------ |
1575| callback | AsyncCallback\<void> | 是   | 回调函数。当订阅指定网络状态变化的通知成功,error为undefined,否则为错误对象。 |
1576
1577**错误码:**
1578
1579| 错误码ID | 错误信息                        |
1580| ------- | -----------------------------  |
1581| 201     | Permission denied.             |
1582| 401     | Parameter error.             |
1583| 2100002 | Operation failed. Cannot connect to service.|
1584| 2100003 | System internal error.         |
1585| 2101008 | The same callback exists.     |
1586| 2101022 | The number of requests exceeded the maximum. |
1587
1588**示例:**
1589
1590```ts
1591import connection from '@ohos.net.connection';
1592import { BusinessError } from "@ohos.base";
1593let netCon: connection.NetConnection = connection.createNetConnection();
1594netCon.register((error: BusinessError) => {
1595  console.log(JSON.stringify(error));
1596});
1597```
1598
1599### unregister
1600
1601unregister(callback: AsyncCallback\<void>): void
1602
1603取消订阅默认网络状态变化的通知。
1604
1605**系统能力**:SystemCapability.Communication.NetManager.Core
1606
1607**参数:**
1608
1609| 参数名   | 类型                 | 必填 | 说明                                                         |
1610| -------- | -------------------- | ---- | ------------------------------------------------------------ |
1611| callback | AsyncCallback\<void> | 是   | 回调函数。当取消订阅指定网络状态变化的通知成功,error为undefined,否则为错误对象。 |
1612
1613**错误码:**
1614
1615| 错误码ID | 错误信息                        |
1616| ------- | -----------------------------  |
1617| 201 | Permission denied.|
1618| 401 | Parameter error.         |
1619| 2100002 | Operation failed. Cannot connect to service.|
1620| 2100003 | System internal error.         |
1621| 2101007 | The callback is not exists.      |
1622
1623**示例:**
1624
1625```ts
1626import connection from '@ohos.net.connection';
1627import { BusinessError } from "@ohos.base";
1628let netCon: connection.NetConnection = connection.createNetConnection();
1629netCon.unregister((error: BusinessError) => {
1630  console.log(JSON.stringify(error));
1631});
1632```
1633
1634### on('netAvailable')
1635
1636on(type: 'netAvailable', callback: Callback\<NetHandle>): void
1637
1638订阅网络可用事件。
1639
1640**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
1641
1642**系统能力**:SystemCapability.Communication.NetManager.Core
1643
1644**参数:**
1645
1646| 参数名   | 类型                               | 必填 | 说明                                                         |
1647| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
1648| type     | string                             | 是   | 订阅事件,固定为'netAvailable'。<br>netAvailable:数据网络可用事件。 |
1649| callback | Callback\<[NetHandle](#nethandle)> | 是   | 回调函数,返回数据网络句柄。|
1650
1651**示例:**
1652
1653```ts
1654import connection from '@ohos.net.connection';
1655import { BusinessError } from "@ohos.base";
1656
1657// 创建NetConnection对象
1658let netCon: connection.NetConnection = connection.createNetConnection();
1659
1660// 先使用register接口注册订阅事件
1661netCon.register((error: BusinessError) => {
1662  console.log(JSON.stringify(error));
1663});
1664
1665// 订阅网络可用事件。调用register后,才能接收到此事件通知
1666netCon.on('netAvailable', (data: connection.NetHandle) => {
1667  console.log(JSON.stringify(data));
1668});
1669
1670// 使用unregister接口取消订阅
1671netCon.unregister((error: BusinessError) => {
1672  console.log(JSON.stringify(error));
1673});
1674```
1675
1676### on('netBlockStatusChange')
1677
1678on(type: 'netBlockStatusChange', callback: Callback&lt;{ netHandle: NetHandle, blocked: boolean }&gt;): void
1679
1680订阅网络阻塞状态事件,使用callback方式作为异步方法。
1681
1682**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
1683
1684**系统能力**:SystemCapability.Communication.NetManager.Core
1685
1686**参数:**
1687
1688| 参数名   | 类型                                                         | 必填 | 说明                                                         |
1689| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1690| type     | string                                                       | 是   | 订阅事件,固定为'netBlockStatusChange'。<br/>netBlockStatusChange:网络阻塞状态事件。 |
1691| callback | Callback&lt;{&nbsp;netHandle:&nbsp;[NetHandle](#nethandle),&nbsp;blocked:&nbsp;boolean&nbsp;}&gt; | 是   | 回调函数,返回数据网络句柄(netHandle),及网络堵塞状态(blocked)。|
1692
1693**示例:**
1694
1695```ts
1696import connection from '@ohos.net.connection';
1697import { BusinessError } from "@ohos.base";
1698
1699// 创建NetConnection对象
1700let netCon: connection.NetConnection = connection.createNetConnection();
1701
1702// 先使用register接口注册订阅事件
1703netCon.register((error: BusinessError) => {
1704  console.log(JSON.stringify(error));
1705});
1706
1707// 订阅网络可用事件。调用register后,才能接收到此事件通知
1708class Value {
1709    netHandle: NetHandle = connection.NetHandle
1710    blocked: boolean = false
1711}
1712netCon.on('netBlockStatusChange', (data: Value) => {
1713  console.log(JSON.stringify(data));
1714});
1715
1716// 使用unregister接口取消订阅
1717netCon.unregister((error: BusinessError) => {
1718  console.log(JSON.stringify(error));
1719});
1720```
1721
1722### on('netCapabilitiesChange')
1723
1724on(type: 'netCapabilitiesChange', callback: Callback\<NetCapabilityInfo>): void
1725
1726订阅网络能力变化事件。
1727
1728**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
1729
1730**系统能力**:SystemCapability.Communication.NetManager.Core
1731
1732**参数:**
1733
1734| 参数名   | 类型                                                         | 必填 | 说明                                                         |
1735| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1736| type     | string                                                       | 是   | 订阅事件,固定为'netCapabilitiesChange'。<br/>netCapabilitiesChange:网络能力变化事件。 |
1737| callback | Callback<[NetCapabilityInfo](#netcapabilityinfo10)> | 是   | 回调函数,返回数据网络句柄(netHandle)和网络的能力信息(netCap)。|
1738
1739**示例:**
1740
1741```ts
1742import connection from '@ohos.net.connection';
1743import { BusinessError } from "@ohos.base";
1744
1745// 创建NetConnection对象
1746let netCon: connection.NetConnection = connection.createNetConnection();
1747
1748// 先使用register接口注册订阅事件
1749netCon.register((error: BusinessError) => {
1750  console.log(JSON.stringify(error));
1751});
1752
1753// 订阅网络能力变化事件。调用register后,才能接收到此事件通知
1754netCon.on('netCapabilitiesChange', (data: connection.NetCapabilityInfo) => {
1755  console.log(JSON.stringify(data));
1756});
1757
1758// 使用unregister接口取消订阅
1759netCon.unregister((error: BusinessError) => {
1760  console.log(JSON.stringify(error));
1761});
1762```
1763
1764### on('netConnectionPropertiesChange')
1765
1766on(type: 'netConnectionPropertiesChange', callback: Callback<{ netHandle: NetHandle, connectionProperties:
1767ConnectionProperties }>): void
1768
1769订阅网络连接信息变化事件。
1770
1771**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
1772
1773**系统能力**:SystemCapability.Communication.NetManager.Core
1774
1775**参数:**
1776
1777| 参数名   | 类型                                                         | 必填 | 说明                                                         |
1778| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
1779| type     | string                                                       | 是   | 订阅事件,固定为'netConnectionPropertiesChange'。<br/>netConnectionPropertiesChange:网络连接信息变化事件。 |
1780| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | 是   | 回调函数,返回数据网络句柄(netHandle)和网络的连接信息(connectionProperties)。|
1781
1782**示例:**
1783
1784```ts
1785import connection from '@ohos.net.connection';
1786import { BusinessError } from "@ohos.base";
1787
1788// 创建NetConnection对象
1789let netCon: connection.NetConnection = connection.createNetConnection();
1790
1791// 先使用register接口注册订阅事件
1792netCon.register((error: BusinessError) => {
1793  console.log(JSON.stringify(error));
1794});
1795
1796class Value {
1797    netHandle: NetHandle = connection.NetHandle
1798    connectionProperties: ConnectionProperties = connection.ConnectionProperties
1799}
1800
1801// 订阅网络可用事件。调用register后,才能接收到此事件通知
1802netCon.on('netConnectionPropertiesChange', (data: Value) => {
1803  console.log(JSON.stringify(data));
1804});
1805
1806// 使用unregister接口取消订阅
1807netCon.unregister((error: BusinessError) => {
1808  console.log(JSON.stringify(error));
1809});
1810```
1811
1812### on('netLost')
1813
1814on(type: 'netLost', callback: Callback\<NetHandle>): void
1815
1816订阅网络丢失事件。
1817
1818**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
1819
1820**系统能力**:SystemCapability.Communication.NetManager.Core
1821
1822**参数:**
1823
1824| 参数名   | 类型                               | 必填 | 说明                                                         |
1825| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ |
1826| type     | string                             | 是   | 订阅事件,固定为'netLost'。<br/>netLost:网络严重中断或正常断开事件。 |
1827| callback | Callback\<[NetHandle](#nethandle)> | 是   | 回调函数,数据网络句柄(netHandle)。|
1828
1829**示例:**
1830
1831```ts
1832import connection from '@ohos.net.connection';
1833import { BusinessError } from "@ohos.base";
1834
1835// 创建NetConnection对象
1836let netCon: connection.NetConnection = connection.createNetConnection();
1837
1838// 先使用register接口注册订阅事件
1839netCon.register((error: BusinessError) => {
1840  console.log(JSON.stringify(error));
1841});
1842
1843// 订阅网络可用事件。调用register后,才能接收到此事件通知
1844netCon.on('netLost', (data: connection.NetHandle) => {
1845  console.log(JSON.stringify(data));
1846});
1847
1848// 使用unregister接口取消订阅
1849netCon.unregister((error: BusinessError) => {
1850  console.log(JSON.stringify(error));
1851});
1852```
1853
1854### on('netUnavailable')
1855
1856on(type: 'netUnavailable', callback: Callback\<void>): void
1857
1858订阅网络不可用事件。
1859
1860**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。
1861
1862**系统能力**:SystemCapability.Communication.NetManager.Core
1863
1864**参数:**
1865
1866| 参数名   | 类型            | 必填 | 说明                                                         |
1867| -------- | --------------- | ---- | ------------------------------------------------------------ |
1868| type     | string          | 是   | 订阅事件,固定为'netUnavailable'。<br/>netUnavailable:网络不可用事件。 |
1869| callback | Callback\<void> | 是   | 回调函数,无返回结果。|
1870
1871**示例:**
1872
1873```ts
1874import connection from '@ohos.net.connection';
1875import { BusinessError } from "@ohos.base";
1876
1877// 创建NetConnection对象
1878let netCon: connection.NetConnection = connection.createNetConnection();
1879
1880// 先使用register接口注册订阅事件
1881netCon.register((error: BusinessError) => {
1882  console.log(JSON.stringify(error));
1883});
1884
1885// 订阅网络不可用事件。调用register后,才能接收到此事件通知
1886netCon.on('netUnavailable', () => {
1887  console.log(JSON.stringify(data));
1888});
1889
1890// 使用unregister接口取消订阅
1891netCon.unregister((error: BusinessError) => {
1892  console.log(JSON.stringify(error));
1893});
1894```
1895
1896## NetHandle
1897
1898数据网络的句柄。
1899
1900在调用NetHandle的方法之前,需要先获取NetHandle对象。
1901
1902**系统能力**:SystemCapability.Communication.NetManager.Core
1903
1904### 属性
1905
1906| 名称    | 类型   | 必填 | 说明                      |
1907| ------ | ------ | --- |------------------------- |
1908| netId  | number | 是  |  网络ID,取值为0代表没有默认网络,其余取值必须大于等于100。 |
1909
1910### bindSocket<sup>9+</sup>
1911
1912bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void
1913
1914将TCPSocket或UDPSocket绑定到当前网络,使用callback方式作为异步方法。
1915
1916**系统能力**:SystemCapability.Communication.NetManager.Core
1917
1918**参数:**
1919
1920| 参数名      | 类型                     | 必填 | 说明                            |
1921| ----------- | ------------------------ | ---- | -------------------------------|
1922| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | 是 | 待绑定的TCPSocket或UDPSocket对象。|
1923| callback    | AsyncCallback\<void>      | 是   | 回调函数。当TCPSocket或UDPSocket成功绑定到当前网络,error为undefined,否则为错误对象。 |
1924
1925**错误码:**
1926
1927| 错误码ID | 错误信息                        |
1928| ------- | -----------------------------  |
1929| 401     | Parameter error.               |
1930| 2100001 | Invalid parameter value.                |
1931| 2100002 | Operation failed. Cannot connect to service.|
1932| 2100003 | System internal error.         |
1933
1934**示例:**
1935
1936```ts
1937import socket from "@ohos.net.socket";
1938import connection from '@ohos.net.connection';
1939import { BusinessError } from '@ohos.base';
1940
1941interface Data {
1942  message: ArrayBuffer,
1943  remoteInfo: socket.SocketRemoteInfo
1944}
1945
1946connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
1947  let tcp = socket.constructTCPSocketInstance();
1948  let udp = socket.constructUDPSocketInstance();
1949  let socketType = "TCPSocket";
1950  if (socketType == "TCPSocket") {
1951    tcp.bind({address:"192.168.xxx.xxx",
1952              port:8080,
1953              family:1} as socket.NetAddress, (error: Error) => {
1954      if (error) {
1955        console.log('bind fail');
1956        return;
1957      }
1958      netHandle.bindSocket(tcp, (error: BusinessError, data: void) => {
1959        if (error) {
1960          console.log(JSON.stringify(error));
1961        } else {
1962          console.log(JSON.stringify(data));
1963        }
1964      });
1965    });
1966  } else {
1967    let callback: (value: Data) => void = (value: Data) => {
1968      console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
1969    };
1970    udp.bind({address:"192.168.xxx.xxx",
1971              port:8080,
1972              family:1} as socket.NetAddress, (error: BusinessError) => {
1973      if (error) {
1974        console.log('bind fail');
1975        return;
1976      }
1977      udp.on('message', (data: Data) => {
1978        console.log(JSON.stringify(data));
1979      });
1980      netHandle.bindSocket(udp, (error: BusinessError, data: void) => {
1981        if (error) {
1982          console.log(JSON.stringify(error));
1983        } else {
1984          console.log(JSON.stringify(data));
1985        }
1986      });
1987    });
1988  }
1989});
1990```
1991
1992### bindSocket<sup>9+</sup>
1993
1994bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>;
1995
1996将TCPSocket或UDPSockett绑定到当前网络,使用Promise方式作为异步方法。
1997
1998**系统能力**:SystemCapability.Communication.NetManager.Core
1999
2000**参数:**
2001
2002| 参数名          | 类型                  | 必填  | 说明                           |
2003| --------------- | --------------------- | ---- | ------------------------------ |
2004| socketParam     | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | 是   | 待绑定的TCPSocket或UDPSocket对象。|
2005
2006**返回值:**
2007
2008| 类型           | 说明                   |
2009| -------------- | ---------------------- |
2010| Promise\<void> | 无返回值的Promise对象。 |
2011
2012**错误码:**
2013
2014| 错误码ID | 错误信息                        |
2015| ------- | -----------------------------  |
2016| 401     | Parameter error.               |
2017| 2100001 | Invalid parameter value.                |
2018| 2100002 | Operation failed. Cannot connect to service.|
2019| 2100003 | System internal error.         |
2020
2021**示例:**
2022
2023```ts
2024import socket from "@ohos.net.socket";
2025import connection from '@ohos.net.connection';
2026import { BusinessError } from '@ohos.base';
2027interface Data {
2028  message: ArrayBuffer,
2029  remoteInfo: socket.SocketRemoteInfo
2030}
2031
2032connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
2033  let tcp = socket.constructTCPSocketInstance();
2034  let udp = socket.constructUDPSocketInstance();
2035  let socketType = "TCPSocket";
2036  if (socketType == "TCPSocket") {
2037    tcp.bind({address:"192.168.xxx.xxx",
2038              port:8080,
2039              family:1} as socket.NetAddress, (error: Error) => {
2040      if (error) {
2041        console.log('bind fail');
2042        return;
2043      }
2044      netHandle.bindSocket(tcp, (error: BusinessError, data: void) => {
2045        if (error) {
2046          console.log(JSON.stringify(error));
2047        } else {
2048          console.log(JSON.stringify(data));
2049        }
2050      });
2051    });
2052  } else {
2053    let callback: (value: Data) => void = (value: Data) => {
2054      console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
2055    }
2056    udp.bind({address:"192.168.xxx.xxx",
2057              port:8080,
2058              family:1} as socket.NetAddress, (error: BusinessError) => {
2059    if (error) {
2060      console.log('bind fail');
2061      return;
2062    }
2063    udp.on('message', (data: Data) => {
2064      console.log(JSON.stringify(data));
2065    });
2066    netHandle.bindSocket(udp, (error: BusinessError, data: void) => {
2067      if (error) {
2068        console.log(JSON.stringify(error));
2069      } else {
2070        console.log(JSON.stringify(data));
2071      }
2072    });
2073  });
2074}
2075});
2076```
2077
2078### getAddressesByName
2079
2080getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void
2081
2082使用对应网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。
2083
2084**需要权限**:ohos.permission.INTERNET
2085
2086**系统能力**:SystemCapability.Communication.NetManager.Core
2087
2088**参数:**
2089
2090| 参数名   | 类型                                              | 必填 | 说明                                                         |
2091| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ |
2092| host     | string                                            | 是   | 需要解析的主机名。                                           |
2093| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是   | 回调函数。当使用对应网络解析主机名成功获取所有IP地址,error为undefined,data为获取到的所有IP地址;否则为错误对象。 |
2094
2095**错误码:**
2096
2097| 错误码ID | 错误信息                        |
2098| ------- | -----------------------------  |
2099| 201     | Permission denied.             |
2100| 401     | Parameter error.               |
2101| 2100001 | Invalid parameter value.                |
2102| 2100002 | Operation failed. Cannot connect to service.|
2103| 2100003 | System internal error.         |
2104
2105**示例:**
2106
2107```ts
2108import connection from '@ohos.net.connection';
2109import { BusinessError } from "@ohos.base";
2110
2111connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
2112  let host = "xxxx";
2113  netHandle.getAddressesByName(host, (error: BusinessError, data: connection.NetAddress[]) => {
2114    console.log(JSON.stringify(error));
2115    console.log(JSON.stringify(data));
2116  });
2117});
2118```
2119
2120### getAddressesByName
2121
2122getAddressesByName(host: string): Promise\<Array\<NetAddress>>
2123
2124使用对应网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。
2125
2126**需要权限**:ohos.permission.INTERNET
2127
2128**系统能力**:SystemCapability.Communication.NetManager.Core
2129
2130**参数:**
2131
2132| 参数名 | 类型   | 必填 | 说明               |
2133| ------ | ------ | ---- | ------------------ |
2134| host   | string | 是   | 需要解析的主机名。 |
2135
2136**返回值:**
2137
2138| 类型                                        | 说明                          |
2139| ------------------------------------------- | ----------------------------- |
2140| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回所有IP地址。 |
2141
2142**错误码:**
2143
2144| 错误码ID | 错误信息                        |
2145| ------- | -----------------------------  |
2146| 201     | Permission denied.             |
2147| 401     | Parameter error.               |
2148| 2100001 | Invalid parameter value.                |
2149| 2100002 | Operation failed. Cannot connect to service.|
2150| 2100003 | System internal error.         |
2151
2152**示例:**
2153
2154```ts
2155import connection from '@ohos.net.connection';
2156
2157connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
2158  let host = "xxxx";
2159  netHandle.getAddressesByName(host).then((data: connection.NetAddress[]) => {
2160    console.log(JSON.stringify(data));
2161  });
2162});
2163```
2164
2165### getAddressByName
2166
2167getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void
2168
2169使用对应网络解析主机名以获取第一个IP地址,使用callback方式作为异步方法。
2170
2171**需要权限**:ohos.permission.INTERNET
2172
2173**系统能力**:SystemCapability.Communication.NetManager.Core
2174
2175**参数:**
2176
2177| 参数名   | 类型                                      | 必填 | 说明                                                         |
2178| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ |
2179| host     | string                                    | 是   | 需要解析的主机名。                                           |
2180| callback | AsyncCallback\<[NetAddress](#netaddress)> | 是   | 回调函数。当使用对应网络解析主机名获取第一个IP地址成功,error为undefined,data为获取的第一个IP地址;否则为错误对象。 |
2181
2182**错误码:**
2183
2184| 错误码ID | 错误信息                        |
2185| ------- | -----------------------------  |
2186| 201     | Permission denied.             |
2187| 401     | Parameter error.               |
2188| 2100001 | Invalid parameter value.                |
2189| 2100002 | Operation failed. Cannot connect to service.|
2190| 2100003 | System internal error.         |
2191
2192**示例:**
2193
2194```ts
2195import connection from '@ohos.net.connection';
2196import { BusinessError } from "@ohos.base";
2197
2198connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
2199  let host = "xxxx";
2200  netHandle.getAddressByName(host, (error: BusinessError, data: connection.NetAddress) => {
2201    console.log(JSON.stringify(error));
2202    console.log(JSON.stringify(data));
2203  });
2204});
2205```
2206
2207### getAddressByName
2208
2209getAddressByName(host: string): Promise\<NetAddress>
2210
2211使用对应网络解析主机名以获取第一个IP地址,使用Promise方式作为异步方法。
2212
2213**需要权限**:ohos.permission.INTERNET
2214
2215**系统能力**:SystemCapability.Communication.NetManager.Core
2216
2217**参数:**
2218
2219| 参数名 | 类型   | 必填 | 说明               |
2220| ------ | ------ | ---- | ------------------ |
2221| host   | string | 是   | 需要解析的主机名。 |
2222
2223**返回值:**
2224
2225| 类型                                | 说明                            |
2226| ----------------------------------- | ------------------------------- |
2227| Promise\<[NetAddress](#netaddress)> | 以Promise形式返回第一个IP地址。 |
2228
2229**错误码:**
2230
2231| 错误码ID | 错误信息                        |
2232| ------- | -----------------------------  |
2233| 201     | Permission denied.             |
2234| 401     | Parameter error.               |
2235| 2100001 | Invalid parameter value.                |
2236| 2100002 | Operation failed. Cannot connect to service.|
2237| 2100003 | System internal error.         |
2238
2239**示例:**
2240
2241```ts
2242import connection from '@ohos.net.connection';
2243
2244connection.getDefaultNet().then((netHandle: connection.NetHandle) => {
2245  let host = "xxxx";
2246  netHandle.getAddressByName(host).then((data: connection.NetAddress) => {
2247    console.log(JSON.stringify(data));
2248  });
2249});
2250```
2251
2252## NetCap
2253
2254网络具体能力。
2255
2256**系统能力**:SystemCapability.Communication.NetManager.Core
2257
2258| 名称                  | 值   | 说明                   |
2259| ------------------------ | ---- | ---------------------- |
2260| NET_CAPABILITY_MMS | 0 | 表示网络可以访问运营商的MMSC(Multimedia&nbsp;Message&nbsp;Service,多媒体短信服务)发送和接收彩信。 |
2261| NET_CAPABILITY_NOT_METERED | 11 | 表示网络流量未被计费。 |
2262| NET_CAPABILITY_INTERNET  | 12   | 表示该网络应具有访问Internet的能力,该能力由网络提供者设置。 |
2263| NET_CAPABILITY_NOT_VPN | 15 | 表示网络不使用VPN(Virtual&nbsp;Private&nbsp;Network,虚拟专用网络)。 |
2264| NET_CAPABILITY_VALIDATED | 16   | 表示该网络访问Internet的能力被网络管理成功验证,该能力由网络管理模块设置。 |
2265
2266## NetBearType
2267
2268网络类型。
2269
2270**系统能力**:SystemCapability.Communication.NetManager.Core
2271
2272| 名称         | 值   | 说明        |
2273| --------------- | ---- | ----------- |
2274| BEARER_CELLULAR | 0    | 蜂窝网络。  |
2275| BEARER_WIFI     | 1    | Wi-Fi网络。 |
2276| BEARER_ETHERNET | 3 | 以太网网络。 |
2277
2278## HttpProxy<sup>10+</sup>
2279
2280网络代理配置信息
2281
2282**系统能力**:SystemCapability.Communication.NetManager.Core
2283
2284| 名称    | 类型   | 必填 | 说明                      |
2285| ------ | ------ | --- |------------------------- |
2286| host  | string | 否  |  代理服务器主机名。 |
2287| port  | number | 否  |  主机端口。 |
2288| exclusionList  | Array<string> | 否  | 不使用代理的主机名列表,主机名支持域名、IP地址以及通配符形式,详细匹配规则如下:<br/>1、域名匹配规则:<br/>(1)完全匹配:代理服务器主机名只要与列表中的任意一个主机名完全相同,就可以匹配。<br/>(2)包含匹配:代理服务器主机名只要包含列表中的任意一个主机名,就可以匹配。<br/>例如,如果在主机名列表中设置了 “ample.com”,则  “ample.com”、“www.ample.com”、“ample.com:80”都会被匹配,而 “www.example.com”、“ample.com.org”则不会被匹配。<br/>2、IP地址匹配规则:代理服务器主机名只要与列表中的任意一个IP地址完全相同,就可以匹配。<br/>3、域名跟IP地址可以同时添加到列表中进行匹配。<br/>4、单个“*”是唯一有效的通配符,当列表中只有通配符时,将与所有代理服务器主机名匹配,表示禁用代理。通配符只能单独添加,不可以与其他域名、IP地址一起添加到列表中,否则通配符将不生效。<br/>5、匹配规则不区分主机名大小写。<br/>6、匹配主机名时,不考虑http和https等协议前缀。 |
2289
2290## NetSpecifier
2291
2292提供承载数据网络能力的实例。
2293
2294**系统能力**:SystemCapability.Communication.NetManager.Core
2295
2296| 名称                     | 类型                                | 必填  | 说明                                                         |
2297| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
2298| netCapabilities         | [NetCapabilities](#netcapabilities) |  是  | 存储数据网络的传输能力和承载类型。                                |
2299| bearerPrivateIdentifier | string                              |  否  |  网络标识符,Wi-Fi网络的标识符是"wifi",蜂窝网络的标识符是"slot0"(对应SIM卡1)。 |
2300
2301## NetCapabilityInfo<sup>10+</sup>
2302
2303提供承载数据网络能力的实例。
2304
2305**系统能力**:SystemCapability.Communication.NetManager.Core
2306
2307| 名称                     | 类型                                | 必填  | 说明                                                         |
2308| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ |
2309| netHandle         | [NetHandle](#nethandle) |  是  | 数据网络句柄。                                |
2310| netCap |  [NetCapabilities](#netcapabilities)       |  否  |  存储数据网络的传输能力和承载类型。 |
2311
2312## NetCapabilities
2313
2314网络的能力集。
2315
2316**系统能力**:SystemCapability.Communication.NetManager.Core
2317
2318| 名称                  | 类型                                | 必填 | 说明                     |
2319| --------------------- | ---------------------------------- | --- | ------------------------ |
2320| linkUpBandwidthKbps   | number                             |  否 |  上行(设备到网络)带宽,0表示无法评估当前网络带宽。  |
2321| linkDownBandwidthKbps | number                             |  否 |  下行(网络到设备)带宽,0表示无法评估当前网络带宽。   |
2322| networkCap            | Array\<[NetCap](#netcap)>           |  否 |  网络具体能力。           |
2323| bearerTypes           | Array\<[NetBearType](#netbeartype)> |  是 |  网络类型。               |
2324
2325## ConnectionProperties
2326
2327网络连接信息。
2328
2329**系统能力**:SystemCapability.Communication.NetManager.Core
2330
2331| 名称           | 类型                               | 必填 |  说明             |
2332| ------------- | ---------------------------------- | ----|---------------- |
2333| interfaceName | string                             | 是 |网卡名称。       |
2334| domains       | string                             | 是 |所属域,默认""。 |
2335| linkAddresses | Array\<[LinkAddress](#linkaddress)> | 是 |链路信息。       |
2336| routes        | Array\<[RouteInfo](#routeinfo)>     | 是 |路由信息。       |
2337| dnses     | Array\<[NetAddress](#netaddress)> | 是 |网络地址,参考[NetAddress](#netaddress)。 |
2338| mtu           | number                             | 是 |最大传输单元。   |
2339
2340## RouteInfo
2341
2342网络路由信息。
2343
2344**系统能力**:SystemCapability.Communication.NetManager.Core
2345
2346| 名称           | 类型                        | 必填 |说明             |
2347| -------------- | --------------------------- | --- |---------------- |
2348| interface      | string                      | 是 |网卡名称。       |
2349| destination    | [LinkAddress](#linkaddress) | 是 |目的地址。       |
2350| gateway        | [NetAddress](#netaddress)   | 是 |网关地址。       |
2351| hasGateway     | boolean                     | 是 |是否有网关。     |
2352| isDefaultRoute | boolean                     | 是 |是否为默认路由。 |
2353
2354## LinkAddress
2355
2356网络链路信息。
2357
2358**系统能力**:SystemCapability.Communication.NetManager.Core
2359
2360| 名称        | 类型                      | 必填 |说明                 |
2361| ------------ | ----------------------- |---- |-------------------- |
2362| address      | [NetAddress](#netaddress) | 是 | 链路地址。           |
2363| prefixLength | number                    | 是 |链路地址前缀的长度。 |
2364
2365## NetAddress
2366
2367网络地址。
2368
2369**系统能力**:SystemCapability.Communication.NetManager.Core
2370
2371| 名称 | 类型 | 必填 | 说明 |
2372| ------- | ------ | -- |------------------------------ |
2373| address | string | 是 |地址。 |
2374| family  | number | 否 |IPv4 = 1,IPv6 = 2,默认IPv4。 |
2375| port    | number | 否 |端口,取值范围\[0, 65535]。 |
2376