1# @ohos.net.connection (Network Connection Management) (System API)
2
3The network connection management module provides basic network management capabilities. You can obtain the default active data network or the list of all active data networks, enable or disable the airplane mode, and obtain network capability information.
4
5> **NOTE**
6> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
7> This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.net.connection (Network Connection Management)](js-apis-net-connection.md).
8
9## Modules to Import
10
11```ts
12import { connection } from '@kit.NetworkKit';
13```
14
15
16## connection.getGlobalHttpProxy<sup>10+</sup>
17
18getGlobalHttpProxy(callback: AsyncCallback\<HttpProxy>): void
19
20Obtains the global HTTP proxy configuration of the network. This API uses an asynchronous callback to return the result.
21
22**System API**: This is a system API.
23
24**System capability**: SystemCapability.Communication.NetManager.Core
25
26**Parameters**
27
28| Name  | Type                                   | Mandatory| Description                                                        |
29| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ |
30| callback | AsyncCallback\<[HttpProxy](js-apis-net-connection.md#httpproxy10)> | Yes  | Callback used to return the result. If the global HTTP proxy configuration of the network is obtained successfully, **error** is **undefined** and **data** is the global HTTP proxy configuration. Otherwise, **error** is an error object.|
31
32**Error codes**
33
34| ID| Error Message                       |
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**Example**
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
57Obtains the global HTTP proxy configuration of the network. This API uses a promise to return the result.
58
59**System API**: This is a system API.
60
61**System capability**: SystemCapability.Communication.NetManager.Core
62
63**Return value**
64
65| Type                             | Description                                 |
66| --------------------------------- | ------------------------------------- |
67| Promise\<[HttpProxy](js-apis-net-connection.md#httpproxy10)> | Promise used to return the result.|
68
69**Error codes**
70
71| ID| Error Message                       |
72| ------- | -----------------------------  |
73| 202     | Non-system applications use system APIs.             |
74| 2100002 | Failed to connect to the service.|
75| 2100003 | System internal error.         |
76
77**Example**
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
94Sets the global HTTP proxy configuration of the network. This API uses an asynchronous callback to return the result.
95
96**System API**: This is a system API.
97
98**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
99
100**System capability**: SystemCapability.Communication.NetManager.Core
101
102**Parameters**
103
104| Name   | Type                   | Mandatory| Description                                                        |
105| --------- | ----------------------- | ---- | ------------------------------------------------------------ |
106| httpProxy | [HttpProxy](js-apis-net-connection.md#httpproxy10) | Yes  | Global HTTP proxy configuration of the network.                                 |
107| callback  | AsyncCallback\<void>    | Yes  | Callback used to return the result. If the global HTTP proxy configuration of the network is set successfully, **error** is **undefined**. Otherwise, **error** is an error object.|
108
109**Error codes**
110
111| ID| Error Message                       |
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**Example**
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
146Sets the global HTTP proxy configuration of the network. This API uses a promise to return the result.
147
148**System API**: This is a system API.
149
150**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
151
152**System capability**: SystemCapability.Communication.NetManager.Core
153
154**Parameters**
155
156| Name   | Type                                                        | Mandatory| Description            |
157| --------- | ------------------------------------------------------------ | ---- | ---------------- |
158| httpProxy | [HttpProxy](js-apis-net-connection.md#httpproxy10)                                      | Yes  | Global HTTP proxy configuration of the network.|
159
160**Return value**
161
162| Type                                       | Description                         |
163| ------------------------------------------- | ----------------------------- |
164| Promise\<void> | Promise that returns no value.|
165
166**Error codes**
167
168| ID| Error Message                       |
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**Example**
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
201Enables the airplane mode. This API uses an asynchronous callback to return the result.
202
203**System API**: This is a system API.
204
205**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
206
207**System capability**: SystemCapability.Communication.NetManager.Core
208
209**Parameters**
210
211| Name  | Type                                             | Mandatory| Description              |
212| -------- | ------------------------------------------------- | ---- | ------------------ |
213| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.        |
214
215**Error codes**
216
217| ID| Error Message                       |
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**Example**
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
240Enables the airplane mode. This API uses a promise to return the result.
241
242**System API**: This is a system API.
243
244**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
245
246**System capability**: SystemCapability.Communication.NetManager.Core
247
248**Return value**
249
250| Type                                       | Description                         |
251| ------------------------------------------- | ----------------------------- |
252| Promise\<void> | Promise that returns no value.|
253
254**Error codes**
255
256| ID| Error Message                       |
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**Example**
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
277Disables the airplane mode. This API uses an asynchronous callback to return the result.
278
279**System API**: This is a system API.
280
281**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
282
283**System capability**: SystemCapability.Communication.NetManager.Core
284
285**Parameters**
286
287| Name  | Type                                             | Mandatory| Description              |
288| -------- | ------------------------------------------------- | ---- | ------------------ |
289| callback | AsyncCallback\<void> | Yes  | Callback used to return the result. If the airplane mode is disabled successfully, **error** is **undefined**. Otherwise, **error** is an error object.|
290
291**Error codes**
292
293| ID| Error Message                       |
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**Example**
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
316Disables the airplane mode. This API uses a promise to return the result.
317
318**System API**: This is a system API.
319
320**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
321
322**System capability**: SystemCapability.Communication.NetManager.Core
323
324**Return value**
325
326| Type                                       | Description                         |
327| ------------------------------------------- | ----------------------------- |
328| Promise\<void> | Promise that returns no value.|
329
330**Error codes**
331
332| ID| Error Message                       |
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**Example**
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
354Resets the network settings to factory defaults. This API uses a promise to return the result.
355
356**System API**: This is a system API.
357
358**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL
359
360**System capability**: SystemCapability.Communication.NetManager.Core
361
362**Return value**
363
364| Type                  | Description                   |
365| ---------------------- | ----------------------- |
366| Promise\<void\>        | Promise that returns no value. |
367
368**Error codes**
369
370| ID| Error Message                                   |
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**Example**
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```
389