1# @ohos.net.connection (网络连接管理)(系统接口)
2
3网络连接管理提供管理网络一些基础能力,包括获取默认激活的数据网络、获取所有激活数据网络列表、开启关闭飞行模式、获取网络能力信息等功能。
4
5> **说明:**
6> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
7> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.net.connection (网络连接管理)](js-apis-net-connection.md)
8
9## 导入模块
10
11```ts
12import { connection } from '@kit.NetworkKit';
13```
14
15
16## connection.getGlobalHttpProxy<sup>10+</sup>
17
18getGlobalHttpProxy(callback: AsyncCallback\<HttpProxy>): void
19
20获取网络的全局代理配置信息,使用callback方式作为异步方法。
21
22**系统接口**:此接口为系统接口。
23
24**系统能力**:SystemCapability.Communication.NetManager.Core
25
26**参数:**
27
28| 参数名   | 类型                                    | 必填 | 说明                                                         |
29| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
30| callback | AsyncCallback\<[HttpProxy](js-apis-net-connection.md#httpproxy10)> | 是   | 回调函数。当成功获取网络的全局代理配置信息时,error为undefined,data为网络的全局代理配置信息;否则为错误对象 |
31
32**错误码:**
33
34| 错误码ID | 错误信息                        |
35| ------- | -----------------------------  |
36| 401     | Parameter error.             |
37| 202     | Non-system applications use system APIs.             |
38| 2100002 | Failed to connect to the service.|
39| 2100003 | System internal error.         |
40
41**示例:**
42
43```ts
44import { connection } from '@kit.NetworkKit';
45import { BusinessError } from '@kit.BasicServicesKit';
46
47connection.getGlobalHttpProxy((error: BusinessError, data: connection.HttpProxy) => {
48  console.info(JSON.stringify(error));
49  console.info(JSON.stringify(data));
50});
51```
52
53## connection.getGlobalHttpProxy<sup>10+</sup>
54
55getGlobalHttpProxy(): Promise\<HttpProxy>;
56
57获取网络的全局代理配置信息,使用Promise方式作为异步方法。
58
59**系统接口**:此接口为系统接口。
60
61**系统能力**:SystemCapability.Communication.NetManager.Core
62
63**返回值:**
64
65| 类型                              | 说明                                  |
66| --------------------------------- | ------------------------------------- |
67| Promise\<[HttpProxy](js-apis-net-connection.md#httpproxy10)> | 以Promise形式返回网络的全局代理配置信息。 |
68
69**错误码:**
70
71| 错误码ID | 错误信息                        |
72| ------- | -----------------------------  |
73| 202     | Non-system applications use system APIs.             |
74| 2100002 | Failed to connect to the service.|
75| 2100003 | System internal error.         |
76
77**示例:**
78
79```ts
80import { connection } from '@kit.NetworkKit';
81import { BusinessError } from '@kit.BasicServicesKit';
82
83connection.getGlobalHttpProxy().then((data: connection.HttpProxy) => {
84  console.info(JSON.stringify(data));
85}).catch((error: BusinessError) => {
86  console.info(JSON.stringify(error));
87});
88```
89
90## connection.setGlobalHttpProxy<sup>10+</sup>
91
92setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\<void>): void
93
94设置网络全局Http代理配置信息,使用callback方式作为异步方法。
95
96**系统接口**:此接口为系统接口。
97
98**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
99
100**系统能力**:SystemCapability.Communication.NetManager.Core
101
102**参数:**
103
104| 参数名    | 类型                    | 必填 | 说明                                                         |
105| --------- | ----------------------- | ---- | ------------------------------------------------------------ |
106| httpProxy | [HttpProxy](js-apis-net-connection.md#httpproxy10) | 是   | 网络全局Http代理配置信息。                                  |
107| callback  | AsyncCallback\<void>    | 是   | 回调函数。当成功设置网络全局Http代理配置信息时,error为undefined,否则为错误对象。|
108
109**错误码:**
110
111| 错误码ID | 错误信息                        |
112| ------- | -----------------------------  |
113| 201     | Permission denied.             |
114| 401     | Parameter error.               |
115| 202     | Non-system applications use system APIs.               |
116| 2100001 | Invalid parameter value.                |
117| 2100002 | Failed to connect to the service.|
118| 2100003 | System internal error.         |
119
120**示例:**
121
122```ts
123import { connection } from '@kit.NetworkKit';
124import { BusinessError } from '@kit.BasicServicesKit';
125
126let exclusionStr = "192.168,baidu.com";
127let exclusionArray = exclusionStr.split(',');
128let httpProxy: connection.HttpProxy = {
129    host: "192.168.xx.xxx",
130    port: 8080,
131    exclusionList: exclusionArray
132}
133connection.setGlobalHttpProxy(httpProxy, (err: BusinessError) => {
134    if (err) {
135        console.error(`setGlobalHttpProxy failed, callback: err->${JSON.stringify(err)}`);
136        return;
137    }
138    console.log(`setGlobalHttpProxy success.`);
139});
140```
141
142## connection.setGlobalHttpProxy<sup>10+</sup>
143
144setGlobalHttpProxy(httpProxy: HttpProxy): Promise\<void>;
145
146设置网络全局Http代理配置信息,使用Promise方式作为异步方法。
147
148**系统接口**:此接口为系统接口。
149
150**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
151
152**系统能力**:SystemCapability.Communication.NetManager.Core
153
154**参数:**
155
156| 参数名    | 类型                                                         | 必填 | 说明             |
157| --------- | ------------------------------------------------------------ | ---- | ---------------- |
158| httpProxy | [HttpProxy](js-apis-net-connection.md#httpproxy10)                                      | 是   | 网络全局Http代理配置信息。 |
159
160**返回值:**
161
162| 类型                                        | 说明                          |
163| ------------------------------------------- | ----------------------------- |
164| Promise\<void> | 无返回值的Promise对象。 |
165
166**错误码:**
167
168| 错误码ID | 错误信息                        |
169| ------- | -----------------------------  |
170| 201     | Permission denied.             |
171| 401     | Parameter error.               |
172| 202     | Non-system applications use system APIs.               |
173| 2100001 | Invalid parameter value.                |
174| 2100002 | Failed to connect to the service.|
175| 2100003 | System internal error.         |
176
177**示例:**
178
179```ts
180import { connection } from '@kit.NetworkKit';
181import { BusinessError } from '@kit.BasicServicesKit';
182
183let exclusionStr = "192.168,baidu.com";
184let exclusionArray = exclusionStr.split(',');
185connection.setGlobalHttpProxy({
186  host: "192.168.xx.xxx",
187  port: 8080,
188  exclusionList: exclusionArray
189} as connection.HttpProxy).then(() => {
190  console.info("success");
191}).catch((error: BusinessError) => {
192  console.info(JSON.stringify(error));
193});
194```
195
196
197## connection.enableAirplaneMode
198
199enableAirplaneMode(callback: AsyncCallback\<void>): void
200
201开启飞行模式,使用callback方式作为异步方法。
202
203**系统接口**:此接口为系统接口。
204
205**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
206
207**系统能力**:SystemCapability.Communication.NetManager.Core
208
209**参数:**
210
211| 参数名   | 类型                                              | 必填 | 说明               |
212| -------- | ------------------------------------------------- | ---- | ------------------ |
213| callback | AsyncCallback\<void> | 是   | 回调函数。         |
214
215**错误码:**
216
217| 错误码ID | 错误信息                        |
218| ------- | -----------------------------  |
219| 201     | Permission denied.             |
220| 202     | Non-system applications use system APIs.              |
221| 401     | Parameter error.               |
222| 2100002 | Failed to connect to the service.|
223| 2100003 | System internal error.         |
224
225**示例:**
226
227```ts
228import { connection } from '@kit.NetworkKit';
229import { BusinessError } from '@kit.BasicServicesKit';
230
231connection.enableAirplaneMode((error: BusinessError) => {
232  console.log(JSON.stringify(error));
233});
234```
235
236## connection.enableAirplaneMode
237
238enableAirplaneMode(): Promise\<void>
239
240开启飞行模式,使用Promise方式作为异步方法。
241
242**系统接口**:此接口为系统接口。
243
244**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
245
246**系统能力**:SystemCapability.Communication.NetManager.Core
247
248**返回值:**
249
250| 类型                                        | 说明                          |
251| ------------------------------------------- | ----------------------------- |
252| Promise\<void> | 无返回值的Promise对象。 |
253
254**错误码:**
255
256| 错误码ID | 错误信息                        |
257| ------- | -----------------------------  |
258| 201     | Permission denied.             |
259| 202     | Non-system applications use system APIs.              |
260| 2100002 | Failed to connect to the service.|
261| 2100003 | System internal error.         |
262
263**示例:**
264
265```ts
266import { connection } from '@kit.NetworkKit';
267
268connection.enableAirplaneMode().then((error: void) => {
269  console.log(JSON.stringify(error));
270});
271```
272
273## connection.disableAirplaneMode
274
275disableAirplaneMode(callback: AsyncCallback\<void>): void
276
277关闭飞行模式,使用callback方式作为异步方法。
278
279**系统接口**:此接口为系统接口。
280
281**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
282
283**系统能力**:SystemCapability.Communication.NetManager.Core
284
285**参数:**
286
287| 参数名   | 类型                                              | 必填 | 说明               |
288| -------- | ------------------------------------------------- | ---- | ------------------ |
289| callback | AsyncCallback\<void> | 是   | 回调函数。当关闭飞行模式成功,error为undefined,否则为错误对象。 |
290
291**错误码:**
292
293| 错误码ID | 错误信息                        |
294| ------- | -----------------------------  |
295| 201     | Permission denied.             |
296| 202     | Non-system applications use system APIs.              |
297| 401     | Parameter error.               |
298| 2100002 | Failed to connect to the service.|
299| 2100003 | System internal error.         |
300
301**示例:**
302
303```ts
304import { connection } from '@kit.NetworkKit';
305import { BusinessError } from '@kit.BasicServicesKit';
306
307connection.disableAirplaneMode((error: BusinessError) => {
308  console.log(JSON.stringify(error));
309});
310```
311
312## connection.disableAirplaneMode
313
314disableAirplaneMode(): Promise\<void>
315
316关闭飞行模式,使用Promise方式作为异步方法。
317
318**系统接口**:此接口为系统接口。
319
320**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
321
322**系统能力**:SystemCapability.Communication.NetManager.Core
323
324**返回值:**
325
326| 类型                                        | 说明                          |
327| ------------------------------------------- | ----------------------------- |
328| Promise\<void> | 无返回值的Promise对象。 |
329
330**错误码:**
331
332| 错误码ID | 错误信息                        |
333| ------- | -----------------------------  |
334| 201     | Permission denied.             |
335| 202     | Non-system applications use system APIs.              |
336| 2100002 | Failed to connect to the service.|
337| 2100003 | System internal error.         |
338
339**示例:**
340
341```ts
342import { connection } from '@kit.NetworkKit';
343
344connection.disableAirplaneMode().then((error: void) => {
345  console.log(JSON.stringify(error));
346});
347```
348
349
350## connection.factoryReset<sup>11+</sup>
351
352factoryReset(): Promise\<void\>
353
354出厂重置网络设置,使用Promise方式作为异步方法。
355
356**系统接口**:此接口为系统接口。
357
358**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL
359
360**系统能力**:SystemCapability.Communication.NetManager.Core
361
362**返回值:**
363
364| 类型                   | 说明                    |
365| ---------------------- | ----------------------- |
366| Promise\<void\>        | 无返回值的Promise对象。  |
367
368**错误码:**
369
370| 错误码ID | 错误信息                                    |
371| ------- | ------------------------------------------  |
372| 201     | Permission denied.                          |
373| 202     | Non-system applications use system APIs.    |
374| 2100002 | Failed to connect to the service.|
375| 2100003 | System internal error.                      |
376
377**示例:**
378
379```ts
380import { connection } from '@kit.NetworkKit';
381import { BusinessError } from '@kit.BasicServicesKit';
382
383connection.factoryReset().then(() => {
384    console.log("success");
385}).catch((error: BusinessError) => {
386    console.log(JSON.stringify(error));
387})
388```