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

!15381 Update ErrorCode Monthly

Merge pull request !15381 from Yangys/monthly_20221018
......@@ -18,32 +18,41 @@ HTTP数据请求功能主要由http模块提供。
| ----------------------------------------- | ----------------------------------- |
| createHttp() | 创建一个http请求。 |
| request() | 根据URL地址,发起HTTP网络请求。 |
| request2()<sup>10+</sup> | 根据URL地址,发起HTTP网络请求并返回流式响应|
| destroy() | 中断请求任务。 |
| on(type: 'headersReceive') | 订阅HTTP Response Header 事件。 |
| off(type: 'headersReceive') | 取消订阅HTTP Response Header 事件。 |
| once\('headersReceive'\)<sup>8+</sup> | 订阅HTTP Response Header 事件,但是只触发一次。|
| on\('dataReceive'\)<sup>10+</sup> | 订阅HTTP流式响应数据接收事件。 |
| off\('dataReceive'\)<sup>10+</sup> | 取消订阅HTTP流式响应数据接收事件。 |
| on\('dataEnd'\)<sup>10+</sup> | 订阅HTTP流式响应数据接收完毕事件。 |
| off\('dataEnd'\)<sup>10+</sup> | 取消订阅HTTP流式响应数据接收完毕事件。 |
| on\('dataProgress'\)<sup>10+</sup> | 订阅HTTP流式响应数据接收进度事件。 |
| off\('dataProgress'\)<sup>10+</sup> | 取消订阅HTTP流式响应数据接收进度事件。 |
## 开发步骤
## request接口开发步骤
1. import需要的http模块。
2. 创建一个HTTP请求,返回一个HttpRequest对象。
3. (可选)订阅HTTP响应头。
4. 根据URL地址,发起HTTP网络请求。
5. (可选)处理HTTP响应头和HTTP网络请求的返回结果。
1. 从@ohos.net.http.d.ts中导入http命名空间。
2. 调用createHttp()方法,创建一个HttpRequest对象。
3. 调用该对象的on()方法,订阅http响应头事件,此接口会比request请求先返回。可以根据业务需要订阅此消息。
4. 调用该对象的request()方法,传入http请求的url地址和可选参数,发起网络请求。
5. 按照实际业务需要,解析返回结果。
6. 调用该对象的off()方法,取消订阅http响应头事件。
7. 当该请求使用完毕时,调用destroy()方法主动销毁。
```js
// 引入包名
import http from '@ohos.net.http';
// 每一个httpRequest对应一个http请求任务,不可复用
// 每一个httpRequest对应一个HTTP请求任务,不可复用
let httpRequest = http.createHttp();
// 用于订阅http响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息
// 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息
// 从API 8开始,使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback)。 8+
httpRequest.on('headersReceive', (header) => {
console.info('header: ' + JSON.stringify(header));
});
httpRequest.request(
// 填写http请求的url地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
"EXAMPLE_URL",
{
method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET
......@@ -55,26 +64,115 @@ httpRequest.request(
extraData: {
"data": "data to send",
},
connectTimeout: 60000, // 可选,默认为60s
readTimeout: 60000, // 可选,默认为60s
expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型
usingCache: true, // 可选,默认为true
priority: 1, // 可选,默认为1
connectTimeout: 60000, // 可选,默认为60000ms
readTimeout: 60000, // 可选,默认为60000ms
usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定
usingProxy: false, //可选,默认不使用网络代理,自API 10开始支持该属性
}, (err, data) => {
if (!err) {
// data.result为http响应内容,可根据业务需要进行解析
console.info('Result:' + data.result);
console.info('code:' + data.responseCode);
// data.header为http响应头,可根据业务需要进行解析
// data.result为HTTP响应内容,可根据业务需要进行解析
console.info('Result:' + JSON.stringify(data.result));
console.info('code:' + JSON.stringify(data.responseCode));
// data.header为HTTP响应头,可根据业务需要进行解析
console.info('header:' + JSON.stringify(data.header));
console.info('cookies:' + data.cookies); // 8+
console.info('cookies:' + JSON.stringify(data.cookies)); // 8+
} else {
console.info('error:' + JSON.stringify(err));
// 该请求不再使用,调用destroy方法主动销毁。
// 取消订阅HTTP响应头事件
httpRequest.off('headersReceive');
// 当该请求使用完毕时,调用destroy方法主动销毁
httpRequest.destroy();
}
}
);
```
## request2接口开发步骤
1. 从@ohos.net.http.d.ts中导入http命名空间。
2. 调用createHttp()方法,创建一个HttpRequest2对象。
3. 调用该对象的on()方法,订阅HTTP流式响应相关事件,此接口会比request请求先返回。可以根据业务需要订阅此消息。
4. 调用该对象的request2()方法,传入http请求的url地址和可选参数,发起网络请求。
5. 调用该对象的off()方法,取消订阅http响应头事件。
6. 当该请求使用完毕时,调用destroy()方法主动销毁。
```js
// 引入包名
import http from '@ohos.net.http';
// 每一个httpRequest对应一个HTTP请求任务,不可复用
let httpRequest2 = http.createHttp();
// 订阅HTTP流式响应数据接收事件的回调函数
function dataReceive_on_callback(data) {
console.info("request2_dataReceive callback function receive len: " + JSON.stringify(data.byteLength));
}
// 订阅HTTP流式响应数据接收进度事件的回调函数
function dataEnd_on_callback() {
console.info(`request2_dataEnd callback function`);
}
// 订阅HTTP流式响应数据接收完毕事件的回调函数
function dataProgress_on_callback(data) {
console.info("request2_dataProgress callback function receive dataProgress: " + JSON.stringify(data));
}
try {
// 用于订阅HTTP响应头,此接口会比request2请求先返回。可以根据业务需要订阅此消息
// 从API 10开始,支持订阅HTTP流式响应相关事件
// 订阅HTTP流式响应数据接收事件
httpRequest2.on("dataReceive", dataReceive_on_callback);
// 订阅HTTP流式响应数据接收进度事件
httpRequest2.on("dataProgress", dataProgress_on_callback);
// 订阅HTTP流式响应数据接收完毕事件
httpRequest2.on("dataEnd", dataEnd_on_callback);
httpRequest2.request2(
// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
"EXAMPLE_URL",
{
method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET
// 开发者根据自身业务需要添加header字段
header: {
'Content-Type': 'application/json'
},
// 当使用POST请求时此字段用于传递内容
extraData: {
"data": "data to send",
},
expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型
usingCache: true, // 可选,默认为true
priority: 1, // 可选,默认为1
connectTimeout: 60000, // 可选,默认为60000ms
readTimeout: 60000, // 可选,默认为60000ms
usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定
usingProxy: false, //可选,默认不使用网络代理,自API 10开始支持该属性
}).then(function () {
console.info("request2 OK!");
// 订阅HTTP流式响应数据接收事件
httpRequest2.off("dataReceive");
console.info("off dataReceive success!");
// 订阅HTTP流式响应数据接收进度事件
httpRequest2.off("dataProgress");
console.info("off dataProgress success!");
// 订阅HTTP流式响应数据接收完毕事件
httpRequest2.off("dataEnd");
console.info("off dataEnd success!");
// 当该请求使用完毕时,调用destroy方法主动销毁
httpRequest2.destroy();
}).catch(function (err) {
console.info("request2 ERROR : " + JSON.stringify(err));
});
} catch (error) {
console.info("request2 ERROR : " + JSON.stringify(error));
}
```
## 相关实例
针对HTTP数据请求,有以下相关实例可供参考:
- [`Http:`数据请求(ArkTS)(API9))](https://gitee.com/openharmony/applications_app_samples/tree/monthly_20221018/Network/Http)
- [`Http:`数据请求(ArkTS)(API9))](https://gitee.com/openharmony/applications_app_samples/tree/master/code/BasicFeature/Connectivity/Http)
- [使用HTTP实现与服务端通信(ArkTS)(API9)](https://gitee.com/openharmony/codelabs/tree/master/NetworkManagement/SmartChatEtsOH)
\ No newline at end of file
......@@ -3,7 +3,6 @@
以太网连接管理主要提供有线网络能力,提供设置有线网络的IP地址,子网掩码,网关,DNS等信息
> **说明:**
>
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 导入模块
......@@ -32,18 +31,37 @@ setIfaceConfig(iface: string, ic: InterfaceConfiguration, callback: AsyncCallbac
| ic | [InterfaceConfiguration](#interfaceconfiguration) | 是 | 要设置的网络接口配置信息 |
| callback | AsyncCallback\<void> | 是 | 回调函数,成功无返回,失败返回对应错误码。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------------------|
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error. |
| 2201005 | The device information does not exist. |
| 2201006 | Device disconnected. |
| 2201007 | Failed to write the user configuration. |
**示例:**
```js
ethernet.setIfaceConfig("eth0", {mode:ethernet.STATIC,ipAddr:"192.168.1.123", routeAddr:"192.168.1.1",
gateAddr:"192.168.1.1", maskAddr:"255.255.255.0", dnsAddr0:"1.1.1.1", dnsAddr1:"2.2.2.2"},
(error) => {
ethernet.setIfaceConfig("eth0", {
mode: 0,
ipAddr: "192.168.xx.xxx",
route: "192.168.xx.xxx",
gateway: "192.168.xx.xxx",
netMask: "255.255.255.0",
dnsServers: "1.1.1.1",
domain: "2.2.2.2"
}, (error) => {
if (error) {
console.log("setIfaceConfig callback error = " + error);
console.log("setIfaceConfig callback error = " + JSON.stringify(error));
} else {
console.log("setIfaceConfig callback ok ");
}
});
});
```
## ethernet.setIfaceConfig
......@@ -71,14 +89,34 @@ setIfaceConfig(iface: string, ic: InterfaceConfiguration): Promise\<void>
| ------------------- | ----------------------------------------------------------- |
| Promise\<void> | 以Promise形式返回执行结果。成功无返回,失败返回对应错误码。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------------------|
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error. |
| 2201005 | The device information does not exist. |
| 2201006 | Device disconnected. |
| 2201007 | Failed to write the user configuration. |
**示例:**
```js
ethernet.setIfaceConfig("eth0", {mode:ethernet.STATIC,ipAddr:"192.168.1.123", routeAddr:"192.168.1.1",
gateAddr:"192.168.1.1", maskAddr:"255.255.255.0", dnsAddr0:"1.1.1.1", dnsAddr1:"2.2.2.2"}).then(() => {
ethernet.setIfaceConfig("eth0", {
mode: 0,
ipAddr: "192.168.xx.xxx",
route: "192.168.xx.xxx",
gateway: "192.168.xx.xxx",
netMask: "255.255.255.0",
dnsServers: "1.1.1.1",
domain: "2.2.2.2"
}).then(() => {
console.log("setIfaceConfig promiss ok ");
}).catch((error) => {
console.log("setIfaceConfig promiss error = " + error);
}).catch(error => {
console.log("setIfaceConfig promiss error = " + JSON.stringify(error));
});
```
......@@ -101,20 +139,31 @@ getIfaceConfig(iface: string, callback: AsyncCallback\<InterfaceConfiguration>):
| iface | string | 是 | 指定网络接口 |
| callback | AsyncCallback\<[InterfaceConfiguration](#interfaceconfiguration)> | 是 | 回调函数,返回指定网络接口信息 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------------------|
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error. |
| 2201005 | The device information does not exist. |
**示例:**
```js
ethernet.getIfaceConfig("eth0", (error, value) => {
if (error) {
console.log("getIfaceConfig callback error = " + error);
console.log("getIfaceConfig callback error = " + JSON.stringify(error));
} else {
console.log("getIfaceConfig callback mode = " + value.mode);
console.log("getIfaceConfig callback ipAddr = " + value.ipAddr);
console.log("getIfaceConfig callback routeAddr = " + value.routeAddr);
console.log("getIfaceConfig callback gateAddr = " + value.gateAddr);
console.log("getIfaceConfig callback maskAddr = " + value.maskAddr);
console.log("getIfaceConfig callback dns0Addr = " + value.dns0Addr);
console.log("getIfaceConfig callback dns1Addr = " + value.dns1Addr);
console.log("getIfaceConfig callback mode = " + JSON.stringify(value.mode));
console.log("getIfaceConfig callback ipAddr = " + JSON.stringify(value.ipAddr));
console.log("getIfaceConfig callback route = " + JSON.stringify(value.route));
console.log("getIfaceConfig callback gateway = " + JSON.stringify(value.gateway));
console.log("getIfaceConfig callback netMask = " + JSON.stringify(value.netMask));
console.log("getIfaceConfig callback dnsServers = " + JSON.stringify(value.dnsServers));
console.log("getIfaceConfig callback domain = " + JSON.stringify(value.domain));
}
});
```
......@@ -143,19 +192,30 @@ getIfaceConfig(iface: string): Promise\<InterfaceConfiguration>
| --------------------------------- | ---------------------------------- |
| Promise\<[InterfaceConfiguration](#interfaceconfiguration)> | 以Promise形式返回接口信息。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------------------|
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error. |
| 2201005 | The device information does not exist. |
**示例:**
```js
ethernet.getIfaceConfig("eth0").then((data) => {
console.log("getIfaceConfig promiss mode = " + data.mode);
console.log("getIfaceConfig promiss ipAddr = " + data.ipAddr);
console.log("getIfaceConfig promiss routeAddr = " + data.routeAddr);
console.log("getIfaceConfig promiss gateAddr = " + data.gateAddr);
console.log("getIfaceConfig promiss maskAddr = " + data.maskAddr);
console.log("getIfaceConfig promiss dns0Addr = " + data.dns0Addr);
console.log("getIfaceConfig promiss dns1Addr = " + data.dns1Addr);
}).catch((error) => {
console.log("getIfaceConfig promiss error = " + error);
console.log("getIfaceConfig promiss mode = " + JSON.stringify(data.mode));
console.log("getIfaceConfig promiss ipAddr = " + JSON.stringify(data.ipAddr));
console.log("getIfaceConfig promiss route = " + JSON.stringify(data.route));
console.log("getIfaceConfig promiss gateway = " + JSON.stringify(data.gateway));
console.log("getIfaceConfig promiss netMask = " + JSON.stringify(data.netMask));
console.log("getIfaceConfig promiss dnsServers = " + JSON.stringify(data.dnsServers));
console.log("getIfaceConfig promiss domain = " + JSON.stringify(data.domain));
}).catch(error => {
console.log("getIfaceConfig promiss error = " + JSON.stringify(error));
});
```
......@@ -178,14 +238,25 @@ isIfaceActive(iface: string, callback: AsyncCallback\<number>): void
| iface | string | 是 | 接口名。为空时代表查询是否存在激活接口 |
| callback | AsyncCallback\<number> | 是 | 回调函数,已激活:1,未激活:0,其他为获取失败错误码。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------------------|
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error. |
| 2201005 | The device information does not exist. |
**示例:**
```js
ethernet.isIfaceActive("eth0", (error, value) => {
if (error) {
console.log("whether2Activate callback error = " + error);
console.log("whether2Activate callback error = " + JSON.stringify(error));
} else {
console.log("whether2Activate callback = " + value);
console.log("whether2Activate callback = " + JSON.stringify(value));
}
});
```
......@@ -214,13 +285,24 @@ isIfaceActive(iface: string): Promise\<number>
| ----------------| ------------------------------------------------------------------ |
| Promise\<number> | 以Promise形式返回获取结果。已激活:1,未激活:0,其他为获取失败错误码。|
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------------------|
| 201 | Permission denied. |
| 401 | Parameter error. |
| 2200001 | Invalid parameter value. |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error. |
| 2201005 | The device information does not exist. |
**示例:**
```js
ethernet.isIfaceActive("eth0").then((data) => {
console.log("isIfaceActive promiss = " + data);
}).catch((error) => {
console.log("isIfaceActive promiss error = " + error);
console.log("isIfaceActive promiss = " + JSON.stringify(data));
}).catch(error => {
console.log("isIfaceActive promiss error = " + JSON.stringify(error));
});
```
......@@ -242,16 +324,24 @@ getAllActiveIfaces(callback: AsyncCallback\<Array\<string>>): void
| -------- | ------------------------------------ | ---- | ------------------------------ |
| callback | AsyncCallback\<Array\<string>> | 是 | 回调函数,返回值为对应接口名。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------------------|
| 201 | Permission denied. |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error. |
**示例:**
```js
ethernet.getAllActiveIfaces((error, value) => {
if (error) {
console.log("getAllActiveIfaces callback error = " + error);
console.log("getAllActiveIfaces callback error = " + JSON.stringify(error));
} else {
console.log("getAllActiveIfaces callback value.length = " + value.length);
console.log("getAllActiveIfaces callback value.length = " + JSON.stringify(value.length));
for (let i = 0; i < value.length; i++) {
console.log("getAllActiveIfaces callback = " + value[i]);
console.log("getAllActiveIfaces callback = " + JSON.stringify(value[i]));
}
}
});
......@@ -275,16 +365,24 @@ getAllActiveIfaces(): Promise\<Array\<string>>
| ------------------------------ | ----------------------------------------------- |
| Promise\<Array\<string>> | 以Promise形式返回获取结果。返回值为对应接口名。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------------------------|
| 201 | Permission denied. |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error. |
**示例:**
```js
ethernet.getAllActiveIfaces().then((data) => {
console.log("getAllActiveIfaces promiss data.length = " + data.length);
console.log("getAllActiveIfaces promiss data.length = " + JSON.stringify(data.length));
for (let i = 0; i < data.length; i++) {
console.log("getAllActiveIfaces promiss = " + data[i]);
console.log("getAllActiveIfaces promiss = " + JSON.stringify(data[i]));
}
}).catch((error) => {
console.log("getAllActiveIfaces promiss error = " + error);
}).catch(error => {
console.log("getAllActiveIfaces promiss error = " + JSON.stringify(error));
});
```
......
......@@ -53,6 +53,13 @@ bind\(address: NetAddress, callback: AsyncCallback<void\>\): void
| address | [NetAddress](#netaddress) | 是 | 目标地址信息,参考[NetAddress](#netaddress)。 |
| callback | AsyncCallback\<void\> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 401 | Parameter error. |
| 201 | Permission denied. |
**示例:**
```js
......@@ -83,6 +90,12 @@ bind\(address: NetAddress\): Promise<void\>
| ------- | ---------------------------------- | ---- | ------------------------------------------------------ |
| address | [NetAddress](#netaddress) | 是 | 目标地址信息,参考[NetAddress](#netaddress)。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 401 | Parameter error. |
| 201 | Permission denied. |
**返回值:**
......@@ -122,6 +135,13 @@ send\(options: UDPSendOptions, callback: AsyncCallback<void\>\): void
| options | [UDPSendOptions](#udpsendoptions) | 是 | UDPSocket发送参数,参考[UDPSendOptions](#udpsendoptions)。 |
| callback | AsyncCallback\<void\> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 401 | Parameter error. |
| 201 | Permission denied. |
**示例:**
```js
......@@ -161,6 +181,13 @@ send\(options: UDPSendOptions\): Promise<void\>
| ------- | ---------------------------------------- | ---- | ------------------------------------------------------------ |
| options | [UDPSendOptions](#udpsendoptions) | 是 | UDPSocket发送参数,参考[UDPSendOptions](#udpsendoptions)。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 401 | Parameter error. |
| 201 | Permission denied. |
**返回值:**
| 类型 | 说明 |
......@@ -265,6 +292,12 @@ getState\(callback: AsyncCallback<SocketStateBase\>\): void
| -------- | ------------------------------------------------------ | ---- | ---------- |
| callback | AsyncCallback<[SocketStateBase](#socketstatebase)> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 201 | Permission denied. |
**示例:**
```js
......@@ -345,6 +378,12 @@ setExtraOptions\(options: UDPExtraOptions, callback: AsyncCallback<void\>\): voi
| options | [UDPExtraOptions](#udpextraoptions) | 是 | UDPSocket连接的其他属性,参考[UDPExtraOptions](#udpextraoptions)。 |
| callback | AsyncCallback\<void\> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 401 | Parameter error. |
| 201 | Permission denied. |
**示例:**
......@@ -398,6 +437,13 @@ setExtraOptions\(options: UDPExtraOptions\): Promise<void\>
| :-------------- | :--------------------------------------------------- |
| Promise\<void\> | 以Promise形式返回设置UDPSocket连接的其他属性的结果。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 401 | Parameter error. |
| 201 | Permission denied. |
**示例:**
```js
......@@ -562,7 +608,6 @@ on\(type: 'error', callback: ErrorCallback\): void
| type | string | 是 | 订阅的事件类型。'error':error事件。 |
| callback | ErrorCallback | 是 | 回调函数。 |
**示例:**
```js
......@@ -667,6 +712,12 @@ Socket的连接信息。
| port | number | 是 | 端口号,范围0~65535。 |
| size | number | 是 | 服务器响应信息的字节长度。 |
## UDP 错误码说明
UDP 其余错误码映射形式为:2301000 + Linux内核错误码。
错误码的详细介绍参见[Socket错误码](../errorcodes/errorcode-net-socket.md)
## socket.constructTCPSocketInstance
constructTCPSocketInstance\(\): TCPSocket
......@@ -709,6 +760,12 @@ bind\(address: NetAddress, callback: AsyncCallback<void\>\): void
| address | [NetAddress](#netaddress) | 是 | 目标地址信息,参考[NetAddress](#netaddress)。 |
| callback | AsyncCallback\<void\> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 401 | Parameter error. |
| 201 | Permission denied. |
**示例:**
......@@ -746,6 +803,13 @@ bind\(address: NetAddress\): Promise<void\>
| :-------------- | :------------------------------------------------------- |
| Promise\<void\> | 以Promise形式返回TCPSocket绑定本机的IP地址和端口的结果。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 401 | Parameter error. |
| 201 | Permission denied. |
**示例:**
```js
......@@ -776,6 +840,13 @@ connect\(options: TCPConnectOptions, callback: AsyncCallback<void\>\): void
| options | [TCPConnectOptions](#tcpconnectoptions) | 是 | TCPSocket连接的参数,参考[TCPConnectOptions](#tcpconnectoptions)。 |
| callback | AsyncCallback\<void\> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 401 | Parameter error. |
| 201 | Permission denied. |
**示例:**
```js
......@@ -812,6 +883,13 @@ connect\(options: TCPConnectOptions\): Promise<void\>
| :-------------- | :--------------------------------------------------------- |
| Promise\<void\> | 以Promise形式返回TCPSocket连接到指定的IP地址和端口的结果。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 401 | Parameter error. |
| 201 | Permission denied. |
**示例:**
```js
......@@ -845,6 +923,13 @@ send\(options: TCPSendOptions, callback: AsyncCallback<void\>\): void
| options | [TCPSendOptions](#tcpsendoptions) | 是 | TCPSocket发送请求的参数,参考[TCPSendOptions](#tcpsendoptions)。 |
| callback | AsyncCallback\<void\> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 401 | Parameter error. |
| 201 | Permission denied. |
**示例:**
```js
......@@ -892,6 +977,13 @@ send\(options: TCPSendOptions\): Promise<void\>
| :-------------- | :------------------------------------------------- |
| Promise\<void\> | 以Promise形式返回通过TCPSocket连接发送数据的结果。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 401 | Parameter error. |
| 201 | Permission denied. |
**示例:**
```js
......@@ -929,6 +1021,11 @@ close\(callback: AsyncCallback<void\>\): void
| -------- | --------------------- | ---- | ---------- |
| callback | AsyncCallback\<void\> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 201 | Permission denied. |
**示例:**
......@@ -960,6 +1057,12 @@ close\(\): Promise<void\>
| :-------------- | :----------------------------------------- |
| Promise\<void\> | 以Promise形式返回关闭TCPSocket连接的结果。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 201 | Permission denied. |
**示例:**
```js
......@@ -992,6 +1095,12 @@ getRemoteAddress\(callback: AsyncCallback<NetAddress\>\): void
| -------- | ------------------------------------------------- | ---- | ---------- |
| callback | AsyncCallback<[NetAddress](#netaddress)> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 201 | Permission denied. |
**示例:**
```js
......@@ -1031,6 +1140,12 @@ getRemoteAddress\(\): Promise<NetAddress\>
| :------------------------------------------ | :------------------------------------------ |
| Promise<[NetAddress](#netaddress)> | 以Promise形式返回获取对端socket地址的结果。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 201 | Permission denied. |
**示例:**
```js
......@@ -1069,6 +1184,11 @@ getState\(callback: AsyncCallback<SocketStateBase\>\): void
| -------- | ------------------------------------------------------ | ---- | ---------- |
| callback | AsyncCallback<[SocketStateBase](#socketstatebase)> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 201 | Permission denied. |
**示例:**
......@@ -1109,6 +1229,11 @@ getState\(\): Promise<SocketStateBase\>
| :----------------------------------------------- | :----------------------------------------- |
| Promise<[SocketStateBase](#socketstatebase)> | 以Promise形式返回获取TCPSocket状态的结果。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 201 | Permission denied. |
**示例:**
......@@ -1149,6 +1274,13 @@ setExtraOptions\(options: TCPExtraOptions, callback: AsyncCallback<void\>\): voi
| options | [TCPExtraOptions](#tcpextraoptions) | 是 | TCPSocket连接的其他属性,参考[TCPExtraOptions](#tcpextraoptions)。 |
| callback | AsyncCallback\<void\> | 是 | 回调函数。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 401 | Parameter error. |
| 201 | Permission denied. |
**示例:**
```js
......@@ -1203,6 +1335,12 @@ setExtraOptions\(options: TCPExtraOptions\): Promise<void\>
| :-------------- | :--------------------------------------------------- |
| Promise\<void\> | 以Promise形式返回设置TCPSocket连接的其他属性的结果。 |
**错误码:**
| 错误码ID | 错误信息 |
| ------- | ----------------------- |
| 401 | Parameter error. |
| 201 | Permission denied. |
**示例:**
......@@ -1304,7 +1442,6 @@ on\(type: 'connect' | 'close', callback: Callback<void\>\): void
| type | string | 是 | 订阅的事件类型。<br />- 'connect':连接事件。<br />- 'close':关闭事件。 |
| callback | Callback\<void\> | 是 | 回调函数。 |
**示例:**
```js
......@@ -1453,6 +1590,12 @@ TCPSocket连接的其他属性。
| reuseAddress | boolean | 否 | 是否重用地址。默认为false。 |
| socketTimeout | number | 否 | 套接字超时时间,单位毫秒(ms)。 |
## TCP 错误码说明
TCP 其余错误码映射形式为:2301000 + Linux内核错误码。
错误码的详细介绍参见[Socket错误码](../errorcodes/errorcode-net-socket.md)
## socket.constructTLSSocketInstance<sup>9+</sup>
constructTLSSocketInstance(): TLSSocket
......@@ -1766,7 +1909,6 @@ connect(options: TLSConnectOptions, callback: AsyncCallback\<void>): void
| 2303104 | Interrupted system call. |
| 2303109 | Bad file number. |
| 2303111 | Resource temporarily unavailable try again. |
| 2303113 | System permission denied. |
| 2303188 | Socket operation on non-socket. |
| 2303191 | Protocol wrong type for socket. |
| 2303198 | Address already in use. |
......@@ -1783,7 +1925,7 @@ connect(options: TLSConnectOptions, callback: AsyncCallback\<void>): void
```js
let tlsTwoWay = socket.constructTLSSocketInstance(); // Two way authentication
tlsTwoWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => {
tlsTwoWay.bind({address: '192.168.xxx.xxx', port: 8080, family: 1}, err => {
if (err) {
console.log('bind fail');
return;
......@@ -1794,14 +1936,14 @@ let options = {
ALPNProtocols: ["spdy/1", "http/1.1"],
address: {
address: "192.168.xx.xxx",
port: xxxx,
port: 8080,
family: 1,
},
secureOptions: {
key: "xxxx",
cert: "xxxx",
ca: ["xxxx"],
passwd: "xxxx",
password: "xxxx",
protocols: [socket.Protocol.TLSv12],
useRemoteCipherPrefer: true,
signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
......@@ -1809,12 +1951,12 @@ let options = {
},
};
tlsTwoWay.connect(options, (err, data) => {
console.error(err);
console.log(data);
console.error("connect callback error"+err);
console.log(JSON.stringify(data));
});
let tlsOneWay = socket.constructTLSSocketInstance(); // One way authentication
tlsOneWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => {
tlsOneWay.bind({address: '192.168.xxx.xxx', port: 8080, family: 1}, err => {
if (err) {
console.log('bind fail');
return;
......@@ -1824,7 +1966,7 @@ tlsOneWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => {
let oneWayOptions = {
address: {
address: "192.168.xxx.xxx",
port: xxxx,
port: 8080,
family: 1,
},
secureOptions: {
......@@ -1833,8 +1975,8 @@ let oneWayOptions = {
},
};
tlsOneWay.connect(oneWayOptions, (err, data) => {
console.error(err);
console.log(data);
console.error("connect callback error"+err);
console.log(JSON.stringify(data));
});
```
......@@ -1866,7 +2008,6 @@ connect(options: TLSConnectOptions): Promise\<void>
| 2303104 | Interrupted system call. |
| 2303109 | Bad file number. |
| 2303111 | Resource temporarily unavailable try again. |
| 2303113 | System permission denied. |
| 2303188 | Socket operation on non-socket. |
| 2303191 | Protocol wrong type for socket. |
| 2303198 | Address already in use. |
......@@ -1883,7 +2024,7 @@ connect(options: TLSConnectOptions): Promise\<void>
```js
let tlsTwoWay = socket.constructTLSSocketInstance(); // Two way authentication
tlsTwoWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => {
tlsTwoWay.bind({address: '192.168.xxx.xxx', port: 8080, family: 1}, err => {
if (err) {
console.log('bind fail');
return;
......@@ -1894,14 +2035,14 @@ let options = {
ALPNProtocols: ["spdy/1", "http/1.1"],
address: {
address: "xxxx",
port: xxxx,
port: 8080,
family: 1,
},
secureOptions: {
key: "xxxx",
cert: "xxxx",
ca: ["xxxx"],
passwd: "xxxx",
password: "xxxx",
protocols: [socket.Protocol.TLSv12],
useRemoteCipherPrefer: true,
signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
......@@ -1909,13 +2050,13 @@ let options = {
},
};
tlsTwoWay.connect(options).then(data => {
console.log(data);
console.log(JSON.stringify(data));
}).catch(err => {
console.error(err);
});
let tlsOneWay = socket.constructTLSSocketInstance(); // One way authentication
tlsOneWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => {
tlsOneWay.bind({address: '192.168.xxx.xxx', port: 8080, family: 1}, err => {
if (err) {
console.log('bind fail');
return;
......@@ -1925,7 +2066,7 @@ tlsOneWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => {
let oneWayOptions = {
address: {
address: "192.168.xxx.xxx",
port: xxxx,
port: 8080,
family: 1,
},
secureOptions: {
......@@ -1934,7 +2075,7 @@ let oneWayOptions = {
},
};
tlsOneWay.connect(oneWayOptions).then(data => {
console.log(data);
console.log(JSON.stringify(data));
}).catch(err => {
console.error(err);
});
......@@ -2263,7 +2404,7 @@ getCipherSuite(): Promise\<Array\<string>>
```js
tls.getCipherSuite().then(data => {
console.log(data);
console.log('getCipherSuite success:' + JSON.stringify(data));
}).catch(err => {
console.error(err);
});
......@@ -2327,7 +2468,7 @@ getSignatureAlgorithms(): Promise\<Array\<string>>
```js
tls.getSignatureAlgorithms().then(data => {
console.log(data);
console.log("getSignatureAlgorithms success" + data);
}).catch(err => {
console.error(err);
});
......@@ -2503,7 +2644,7 @@ TLS安全相关操作,其中ca证书为必选参数,其他参数为可选参
| ca | string \| Array\<string> | 是 | 服务端的ca证书,用于认证校验服务端的数字证书。|
| cert | string | 否 | 本地客户端的数字证书。 |
| key | string | 否 | 本地数字证书的私钥。 |
| passwd | string | 否 | 读取私钥的密码。 |
| password | string | 否 | 读取私钥的密码。 |
| protocols | [Protocol](#protocol9) \|Array\<[Protocol](#protocol9)> | 否 | TLS的协议版本。 |
| useRemoteCipherPrefer | boolean | 否 | 优先使用对等方的密码套件。 |
| signatureAlgorithms | string | 否 | 通信过程中的签名算法。 |
......
......@@ -45,11 +45,11 @@ fetch(Object): void
## FetchResponse
| 参数名 | 类型 | 说明 |
| -------- | -------- | -------- |
| code | number | 表示服务器的状态code。 |
| data | string \| Object | 返回数据类型由responseType确定,详见表 responseType与success中data关系。 |
| headers | Object | 表示服务器response的所有header。 |
| 参数名 | 类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
| code | number | 是 | 否 | 表示服务器的状态code。 |
| data | string \| Object | 是 | 否 | 返回数据类型由responseType确定,详见表 responseType与success中data关系。 |
| headers | Object | 是 | 否 | 表示服务器response的所有header。 |
**表2** responseType与success中data关系
......
......@@ -122,7 +122,9 @@ export default {
## NetworkResponse
| 参数名 | 类型 | 说明 |
| -------- | -------- | -------- |
| metered | boolean | 是否按照流量计费。 |
| type | string | 网络类型,可能的值有2g,3g,4g,5g,wifi,none等。 |
\ No newline at end of file
**系统能力:** SystemCapability.Communication.NetManager.Core
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| metered | boolean | 否 |是否按照流量计费。 |
| type | string | 是|网络类型,可能的值有2g,3g,4g,5g,wifi,none等。 |
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册