未验证 提交 a7b5fd9c 编写于 作者: O openharmony_ci 提交者: Gitee

!10966 翻译已完成9955

Merge pull request !10966 from shawn_he/9955-a
......@@ -61,11 +61,37 @@ connection.getDefaultNet().then(function (netHandle) {
})
```
## connection.getDefaultNetSync
getDefaultNetSync(): NetHandle;
Obtains the default active data network in synchronous mode.
**Required permission**: ohos.permission.GET_NETWORK_INFO
**System capability**: SystemCapability.Communication.NetManager.Core
**Return value**
| Type | Description |
| --------- | ---------------------------------- |
| NetHandle | Handle of the default active data network.|
**Example**
```js
let netHandle = connection.getDefaultNetSync();
```
## connection.hasDefaultNet
hasDefaultNet(callback: AsyncCallback\<boolean>): void
Checks whether the default data network is activated. This API uses an asynchronous callback to return the result.
The default network priority is as follows: Ethernet > Wi-Fi > cellular. When only one network is connected, it is treated as the default data network.
**Required permission**: ohos.permission.GET_NETWORK_INFO
**System capability**: SystemCapability.Communication.NetManager.Core
......@@ -89,6 +115,9 @@ connection.hasDefaultNet(function (error, has) {
hasDefaultNet(): Promise\<boolean>
Checks whether the default data network is activated. This API uses a promise to return the result.
The default network priority is as follows: Ethernet > Wi-Fi > cellular. When only one network is connected, it is treated as the default data network.
**Required permission**: ohos.permission.GET_NETWORK_INFO
**System capability**: SystemCapability.Communication.NetManager.Core
......@@ -110,7 +139,7 @@ connection.hasDefaultNet().then(function (has) {
getAllNets(callback: AsyncCallback&lt;Array&lt;NetHandle&gt;&gt;): void
Obtains the list of all connected networks. This API uses an asynchronous callback to return the result.
Obtains the list of all active data networks. This API uses an asynchronous callback to return the result.
**Required permission**: ohos.permission.GET_NETWORK_INFO
......@@ -136,7 +165,7 @@ connection.getAllNets(function (error, nets) {
getAllNets(): Promise&lt;Array&lt;NetHandle&gt;&gt;
Obtains the list of all connected networks. This API uses a promise to return the result.
Obtains the list of all active data networks. This API uses a promise to return the result.
**Required permission**: ohos.permission.GET_NETWORK_INFO
......@@ -160,7 +189,7 @@ connection.getAllNets().then(function (nets) {
getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<ConnectionProperties>): void
Obtains connection properties of the network corresponding to given network handle. This API uses an asynchronous callback to return the result.
Obtains connection properties of the network corresponding to the given network handle. This API uses an asynchronous callback to return the result.
**Required permission**: ohos.permission.GET_NETWORK_INFO
......@@ -280,7 +309,7 @@ connection.getDefaultNet().then(function (netHandle) {
reportNetConnected(netHandle: NetHandle, callback: AsyncCallback&lt;void&gt;): void
Reports connection of the data network to the network management module. This API uses an asynchronous callback to return the result. If this API is called, the application considers that the network connection state (**ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED**) is inconsistent with that in the network management module.
Reports connection of the data network. This API uses an asynchronous callback to return the result.
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
......@@ -308,7 +337,7 @@ connection.getDefaultNet().then(function (netHandle) {
reportNetConnected(netHandle: NetHandle): Promise&lt;void&gt;
Reports connection of the data network to the network management module. This API uses a promise to return the result. If this API is called, the application considers that the network connection state (**ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED**) is inconsistent with that in the network management module.
Reports connection of the data network. This API uses a promise to return the result.
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
......@@ -341,7 +370,7 @@ connection.getDefaultNet().then(function (netHandle) {
reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback&lt;void&gt;): void
Reports disconnection of the data network to the network management module. This API uses an asynchronous callback to return the result. If this API is called, the application considers that the network connection state (**ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED**) is inconsistent with that in the network management module.
Reports disconnection of the data network. This API uses an asynchronous callback to return the result.
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
......@@ -369,8 +398,7 @@ connection.getDefaultNet().then(function (netHandle) {
reportNetDisconnected(netHandle: NetHandle): Promise&lt;void&gt;
Reports disconnection of the data network to the network management module. This API uses a promise to return the result. If this API is called, the application considers that the network connection state (**ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED**) is inconsistent with that in the network management module.
Reports disconnection of the data network. This API uses a promise to return the result.
**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET
......@@ -559,7 +587,7 @@ connection.disableAirplaneMode().then(function (error) {
createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection
Creates a **NetConnection** object. **netSpecifier** specifies the network, and **timeout** specifies the timeout interval in ms. **timeout** is configurable only when **netSpecifier** is specified. If neither of them is present, the default network is used.
Obtains the handle of the network specified by **netSpecifier**.
**System capability**: SystemCapability.Communication.NetManager.Core
......@@ -793,6 +821,108 @@ Before invoking NetHandle APIs, call **getNetHandle** to obtain a **NetHandle**
| ------ | ------ | ------------------------- |
| netId | number | Network ID. The value must be greater than or equal to 100.|
### bindSocket
bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void;
Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses an asynchronous callback to return the result.
**Required permission**: ohos.permission.GET_NETWORK_INFO
**System capability**: SystemCapability.Communication.NetManager.Core
**Parameters**
| Name | Type | Mandatory| Description |
| ----------- | ------------------------ | ---- | -------------------------------|
| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes| **TCPSocket** or **UDPSocket** object.|
| callback | AsyncCallback\<void> | Yes | Callback used to return the result. |
**Example**
```js
connection.getDefaultNet().then(function (netHandle) {
var tcp = socket.constructTCPSocketInstance();
var udp = socket.constructUDPSocketInstance();
let socketType = "xxxx";
if (socketType == "TCPSocket") {
tcp.bind({
address: "xxxx", port: xxxx, family: xxxx
}, err => {
netHandle.bindSocket(tcp, function (error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
})
} else {
udp.on('message', callback);
udp.bind({
address: "xxxx", port: xxxx, family: xxxx
}, err => {
udp.on('message', (data) => {
console.log(JSON.stringify(data))
});
netHandle.bindSocket(udp, function (error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
});
})
}
}
```
### bindSocket
bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>;
Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses a promise to return the result.
**Required permission**: ohos.permission.GET_NETWORK_INFO
**System capability**: SystemCapability.Communication.NetManager.Core
**Parameters**
| Name | Type | Mandatory | Description |
| --------------- | --------------------- | ---- | ------------------------------ |
| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes | **TCPSocket** or **UDPSocket** object.|
**Return value**
| Type | Description |
| -------------- | ---------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
connection.getDefaultNet().then(function (netHandle) {
var tcp = socket.constructTCPSocketInstance();
var udp = socket.constructUDPSocketInstance();
let socketType = "xxxx";
if(socketType == "TCPSocket") {
tcp.bind({
address: "xxxx", port: xxxx, family: xxxx
}, err => {
netHandle.bindSocket(tcp).then(err, data) {
console.log(JSON.stringify(data))
})
} else {
udp.on('message', callback);
udp.bind({
address: "xxxx", port: xxxx, family: xxxx
}, err => {
udp.on('message', (data) => {
console.log(JSON.stringify(data))
});
netHandle.bindSocket(tcp).then(err, data) {
console.log(JSON.stringify(data))
});
})
}
}
```
### getAddressesByName
getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void
......@@ -951,9 +1081,9 @@ Defines the network capability.
| ------------------------ | ---- | ---------------------- |
| NET_CAPABILITY_MMS | 0 | The network can connect to the carrier's Multimedia Messaging Service Center (MMSC) to send and receive multimedia messages.|
| NET_CAPABILITY_NOT_METERED | 11 | The network traffic is not metered.|
| NET_CAPABILITY_INTERNET | 12 | The network has the Internet access capability, which is set by the network provider.|
| NET_CAPABILITY_INTERNET | 12 | The network can connect to the Internet.|
| NET_CAPABILITY_NOT_VPN | 15 | The network does not use a Virtual Private Network (VPN).|
| NET_CAPABILITY_VALIDATED | 16 | The Internet access capability of the network is successfully verified by the network management module. |
| NET_CAPABILITY_VALIDATED | 16 | The network is available. |
## NetBearType
......
# TLSSocket
The Transport Layer Security (TLS) protocol is designed to help protect the privacy of information at the transport layer. TLSSocket is an extension to socket communication. It provides higher security than socket communication by adding a security protection layer, which consists of the following submodules: key, certificate, and communication.
> **NOTE**
>
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```js
import socket from '@ohos.net.tlssocket'
```
## socket.constructTLSSocketInstance
constructTLSSocketInstance(): TLSSocket
Creates a **TLSSocket** object.
**Required permissions**: ohos.permission.INTERNET
**System capability**: SystemCapability.Communication.NetStack
**Example**
```js
let tlssocket = socket.constructTLSSocketInstance();
```
## tlssocket.connect
connect(options: TLSConnectOptions, callback: AsyncCallback\<void>): void
Sets up a TLSSocket connection, and creates and initializes a TLS session. During this process, a TLS/SSL handshake is performed between the application and the server to implement data transmission. This API uses an asynchronous callback to return the result.
**Parameters**
| Name | Type | Mandatory| Description|
| -------- | ---------------------------------------| ----| --------------- |
| options | [TLSConnectOptions](#tlsconnectoptions) | Yes | Parameters required for the connection.|
| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, the return result is empty. If the operation fails, an error code is returned.|
**Example**
```js
let options = {
ALPNProtocols: ["spdy/1", "http/1.1"],
address: {
address: "xxx",
port: "xxxx",
family: 1,
},
secureOptions: {
key: "xxxx",
cert: "xxxx",
ca: ["xxxx"],
passwd: "xxxx",
protocols: "TlsV1_2",
useRemoteCipherPrefer: true,
signatureAlgorithms: SHA256,
cipherSuites: AES256-SHA256,
},
};
tlssocket.connect(options, (err, data) => {
console.info(err);
console.info(data);
});
```
## tlssocket.connect
connect(options: TLSConnectOptions): Promise\<void>;
Sets up a TLSSocket connection, and creates and initializes a TLS session. During this process, a TLS/SSL handshake is performed between the application and the server to implement data transmission. This API uses a promise to return the result.
**Required permissions**: ohos.permission.INTERNET
**System capability**: SystemCapability.Communication.NetStack
**Parameters**
| Name | Type | Mandatory| Description|
| -------- | --------------------------------------| ----| --------------- |
| options | [TLSConnectOptions](#tlsconnectoptions) | Yes | Parameters required for the connection.|
**Return value**
| Type | Description |
| ------------------------------------------- | ----------------------------- |
| Promise\<void> | Promise used to return the result. If the operation is successful, the return result is empty. If the operation fails, an error code is returned.|
**Example**
```js
let options = {
ALPNProtocols: ["spdy/1", "http/1.1"],
address: {
address: "xxxx",
port: "xxxx",
family: 1,
},
secureOptions: {
key: "xxxx",
cert: "xxxx",
ca: ["xxxx"],
passwd: "xxxx",
protocols: "TlsV1_2",
useRemoteCipherPrefer: true,
signatureAlgorithms: SHA256,
cipherSuites: AES256-SHA256,
},
};
tlssocket.connect(options).then(data => {
console.info(data);
}).catch(err => {
console.error(err);
});
```
## tlssocket.getCertificate
getCertificate(callback: AsyncCallback\<string>): void;
Obtains the local digital certificate after a TLSSocket connection is established. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.INTERNET
**System capability**: SystemCapability.Communication.NetStack
**Parameters**
| Name | Type | Mandatory| Description|
| -------- | ----------------------------------------| ---- | ---------------|
| callback | AsyncCallback\<string> | Yes | Callback used to return the result.|
**Example**
```js
tlssocket.getCertificate((err, data) => {
if (err) {
console.log("getCertificate callback error = " + err);
} else {
console.log("getCertificate callback = " + data);
}
});
```
## tlssocket.getCertificate
getCertificate():Promise\<string>;
Obtains the local digital certificate after a TLSSocket connection is established. This API uses a promise to return the result.
**Required permissions**: ohos.permission.INTERNET
**System capability**: SystemCapability.Communication.NetStack
**Return value**
| Type | Description |
| -------------- | -------------------- |
| Promise\<string> | Promise used to return the result.|
**Example**
```js
tlssocket.getCertificate().then(data => {
console.info(data);
}).catch(err => {
console.error(err);
});
```
## tlssocket.getRemoteCertificate
getRemoteCertificate(callback: AsyncCallback\<string>): void;
Obtains the remote digital certificate after a TLSSocket connection is established. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.INTERNET
**System capability**: SystemCapability.Communication.NetStack
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ----------------------------------------| ---- | ---------------|
| callback | AsyncCallback\<string> | Yes | Callback used to return the result.|
**Example**
```js
tlssocket.getRemoteCertificate((err, data) => {
if (err) {
console.log("getRemoteCertificate callback error = " + err);
} else {
console.log("getRemoteCertificate callback = " + data);
}
});
```
## tlssocket.getRemoteCertificate
getRemoteCertificate():Promise\<string>;
Obtains the remote digital certificate after a TLSSocket connection is established. This API uses a promise to return the result.
**Required permissions**: ohos.permission.INTERNET
**System capability**: SystemCapability.Communication.NetStack
**Return value**
| Type | Description |
| -------------- | -------------------- |
| Promise\<string> | Promise used to return the result.|
**Example**
```js
tlssocket.getRemoteCertificate().then(data => {
console.info(data);
}).catch(err => {
console.error(err);
});
```
## tlssocket.getProtocol
getProtocol(callback: AsyncCallback\<string>): void;
Obtains the communication protocol after a TLSSocket connection is established. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.INTERNET
**System capability**: SystemCapability.Communication.NetStack
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ----------------------------------------| ---- | ---------------|
| callback | AsyncCallback\<string> | Yes | Callback used to return the result. |
**Example**
```js
tlssocket.getProtocol((err, data) => {
if (err) {
console.log("getProtocol callback error = " + err);
} else {
console.log("getProtocol callback = " + data);
}
});
```
## tlssocket.getProtocol
getProtocol():Promise\<string>;
Obtains the communication protocol after a TLSSocket connection is established. This API uses a promise to return the result.
**Required permissions**: ohos.permission.INTERNET
**System capability**: SystemCapability.Communication.NetStack
**Return value**
| Type | Description |
| -------------- | -------------------- |
| Promise\<string> | Promise used to return the result.|
**Example**
```js
tlssocket.getProtocol().then(data => {
console.info(data);
}).catch(err => {
console.error(err);
});
```
## tlssocket.getCipherSuites
getCipherSuites(callback: AsyncCallback\<Array\<string>>): void;
Obtains the cipher suites supported by both parties after a TLSSocket connection is established. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.INTERNET
**System capability**: SystemCapability.Communication.NetStack
**Parameters**
| Name | Type | Mandatory| Description|
| -------- | ----------------------------------------| ---- | ---------------|
| callback | AsyncCallback\<Array\<string>> | Yes | Callback used to return the result. |
**Example**
```js
tlssocket.getCipherSuites((err, data) => {
if (err) {
console.log("getCipherSuites callback error = " + err);
} else {
console.log("getCipherSuites callback = " + data);
}
});
```
## tlssocket.getCipherSuites
getCipherSuites(): Promise\<Array\<string>>;
Obtains the cipher suites supported by both parties after a TLSSocket connection is established. This API uses a promise to return the result.
**Required permissions**: ohos.permission.INTERNET
**System capability**: SystemCapability.Communication.NetStack
**Return value**
| Type | Description |
| ---------------------- | --------------------- |
| Promise\<Array\<string>> | Promise used to return the result.|
**Example**
```js
tlssocket.getCipherSuites().then(data => {
console.info(data);
}).catch(err => {
console.error(err);
});
```
## tlssocket.getSignatureAlgorithms
getSignatureAlgorithms(callback: AsyncCallback\<Array\<string>>): void;
Obtains the signing algorithms supported by both parties after a TLSSocket connection is established. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.INTERNET
**System capability**: SystemCapability.Communication.NetStack
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -------------------------------------| ---- | ---------------|
| callback | AsyncCallback\<Array\<string>> | Yes | Callback used to return the result. |
**Example**
```js
tlssocket.getSignatureAlgorithms((err, data) => {
if (err) {
console.log("getSignatureAlgorithms callback error = " + err);
} else {
console.log("getSignatureAlgorithms callback = " + data);
}
});
```
## tlssocket.getSignatureAlgorithms
getSignatureAlgorithms(): Promise\<Array\<string>>;
Obtains the signing algorithms supported by both parties after a TLSSocket connection is established. This API uses a promise to return the result.
**Required permissions**: ohos.permission.INTERNET
**System capability**: SystemCapability.Communication.NetStack
**Return value**
| Type | Description |
| ---------------------- | -------------------- |
| Promise\<Array\<string>> | Promise used to return the result.|
**Example**
```js
tlssocket.getSignatureAlgorithms().then(data => {
console.info(data);
}).catch(err => {
console.error(err);
});
```
## tlssocket.close
close(callback: AsyncCallback\<void>): void;
Closes a TLSSocket connection. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.INTERNET
**System capability**: SystemCapability.Communication.NetStack
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | -----------------------------| ---- | ---------------|
| callback | AsyncCallback\<void> | Yes | Callback used to return the result. |
**Example**
```js
tlssocket.close((err) => {
if (err) {
console.log("close callback error = " + err);
} else {
console.log("close success");
}
});
```
## tlssocket.close
close(): Promise\<void>;
Closes a TLSSocket connection. This API uses a promise to return the result.
**Required permissions**: ohos.permission.INTERNET
**System capability**: SystemCapability.Communication.NetStack
**Return value**
| Type | Description |
| -------------- | -------------------- |
| Promise\<void> | Promise used to return the result.|
**Example**
```js
tlssocket.close().then(() =>
console.log("close success");
}).catch(err => {
console.error(err);
});
```
## TLSConnectOptions
Defines a TLSSocket connection.
**System capability**: SystemCapability.Communication.NetStack
| Name | Type | Description |
| -------------- | ------------------------------------- | -------------- |
| address | [NetAddress](#netaddress) | Gateway address. |
| secureOptions | [TLSSecureOptions](#tlssecureoptions) | TLS security options.|
| ALPNProtocols | Array\<string> | Application Layer Protocol Negotiation (ALPN) protocols. |
## NetAddress
Defines a network address.
**System capability**: SystemCapability.Communication.NetStack
| Name | Type | Description |
| ------- | ------ | ---------------------------- |
| address | string | Network address. |
| family | number | Address family identifier. The value is **1** for IPv4 and **2** for IPv6. The default value is **1**.|
| port | number | Port number. The value ranges from **0** to **65535**. |
## TLSSecureOptions
Defines TLS security options.
**System capability**: SystemCapability.Communication.NetStack
| Name | Type | Description |
| --------------------- | ---------------------- | ---------------------- |
| ca | string \| Array\<string> | CA certificate. |
| cert | string | Local digital certificate. |
| key | string | Private key of the local digital certificate. |
| passwd | string | Password. |
| protocols | string | Protocols. |
| useRemoteCipherPrefer | boolean | Whether to use the remote cipher suite preferentially.|
| signatureAlgorithms | string | Signing algorithms. |
| cipherSuites | string | Cipher suites. |
......@@ -68,9 +68,13 @@ foundation/communication/
### Sharing a Network
1. Import the network sharing namespace from **@ohos.net.sharing**.
2. Set the network sharing type.
3. Start network sharing.
4. Stop network sharing.
```
// Import the network sharing namespace.
import sharing from '@ohos.net.sharing';
......@@ -89,10 +93,15 @@ sharing.stopSharing(this.sharingType,(err)=>{
### Initiating a Network Request
1. Import the HTTP namespace from **@ohos.net.http.d.ts**.
2. Call **createHttp()** to create an **HttpRequest** object.
3. Call **httpRequest.on()** to subscribe to an HTTP response header. This method returns a response earlier than the request. You can subscribe to HTTP response header events based on service requirements.
4. Call **httpRequest.request()** to initiate a network request. You need to pass in the URL and optional parameters of the HTTP request.
5. Parse the returned result based on service requirements.
6. Call **httpRequest.destroy()** to release resources after the request is processed.
```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册