diff --git a/zh-cn/application-dev/connectivity/http-request.md b/zh-cn/application-dev/connectivity/http-request.md
index 0c427cbb9e5bc3661f5f7957114aacc384aab32e..ff6ec5ed4113c8c9c14cb6a1613dd86dab7ff232 100644
--- a/zh-cn/application-dev/connectivity/http-request.md
+++ b/zh-cn/application-dev/connectivity/http-request.md
@@ -18,32 +18,41 @@ HTTP数据请求功能主要由http模块提供。
| ----------------------------------------- | ----------------------------------- |
| createHttp() | 创建一个http请求。 |
| request() | 根据URL地址,发起HTTP网络请求。 |
+| request2()10+ | 根据URL地址,发起HTTP网络请求并返回流式响应|
| destroy() | 中断请求任务。 |
| on(type: 'headersReceive') | 订阅HTTP Response Header 事件。 |
| off(type: 'headersReceive') | 取消订阅HTTP Response Header 事件。 |
+| once\('headersReceive'\)8+ | 订阅HTTP Response Header 事件,但是只触发一次。|
+| on\('dataReceive'\)10+ | 订阅HTTP流式响应数据接收事件。 |
+| off\('dataReceive'\)10+ | 取消订阅HTTP流式响应数据接收事件。 |
+| on\('dataEnd'\)10+ | 订阅HTTP流式响应数据接收完毕事件。 |
+| off\('dataEnd'\)10+ | 取消订阅HTTP流式响应数据接收完毕事件。 |
+| on\('dataProgress'\)10+ | 订阅HTTP流式响应数据接收进度事件。 |
+| off\('dataProgress'\)10+ | 取消订阅HTTP流式响应数据接收进度事件。 |
## 开发步骤
-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,19 +64,26 @@ 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();
}
}
@@ -76,5 +92,5 @@ httpRequest.request(
## 相关实例
针对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
diff --git a/zh-cn/application-dev/reference/apis/js-apis-http.md b/zh-cn/application-dev/reference/apis/js-apis-http.md
index 16696b77fd623cede7edb72a05bafdb1cca37bb0..1cdf224165953ca89bc5d02298ff045a4f45b23c 100644
--- a/zh-cn/application-dev/reference/apis/js-apis-http.md
+++ b/zh-cn/application-dev/reference/apis/js-apis-http.md
@@ -16,6 +16,7 @@ import http from '@ohos.net.http';
## 完整示例
```js
+// 引入包名
import http from '@ohos.net.http';
// 每一个httpRequest对应一个HTTP请求任务,不可复用
@@ -44,16 +45,19 @@ httpRequest.request(
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);
+ 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));
+ // 取消订阅HTTP响应头事件
+ httpRequest.off('headersReceive');
// 当该请求使用完毕时,调用destroy方法主动销毁。
httpRequest.destroy();
}
@@ -103,6 +107,22 @@ request\(url: string, callback: AsyncCallback\\):void
| url | string | 是 | 发起网络请求的URL地址。 |
| callback | AsyncCallback\<[HttpResponse](#httpresponse)\> | 是 | 回调函数。 |
+**错误码:**
+
+| 错误码ID | 错误信息 |
+|---------|-------------------------------------------------------|
+| 401 | Parameter error. |
+| 201 | Permission denied. |
+| 2300003 | URL using bad/illegal format or missing URL. |
+| 2300007 | Couldn't connect to server. |
+| 2300028 | Timeout was reached. |
+| 2300052 | Server returned nothing (no headers, no data). |
+| 2300999 | Unknown Other Error. |
+
+>**错误码说明:**
+> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
+> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
+
**示例:**
```js
@@ -120,7 +140,7 @@ httpRequest.request("EXAMPLE_URL", (err, data) => {
### request
-request\(url: string, options: HttpRequestOptions, callback: AsyncCallback\):void
+request\(url: string, options: HttpRequestOptions, callback: AsyncCallback\\):void
根据URL地址和相关配置项,发起HTTP网络请求,使用callback方式作为异步方法。
@@ -136,6 +156,46 @@ request\(url: string, options: HttpRequestOptions, callback: AsyncCallback | 是 | 回调函数。 |
+**错误码:**
+
+| 错误码ID | 错误信息 |
+|---------|-------------------------------------------------------|
+| 401 | Parameter error. |
+| 201 | Permission denied. |
+| 2300001 | Unsupported protocol. |
+| 2300003 | URL using bad/illegal format or missing URL. |
+| 2300005 | Couldn't resolve proxy name. |
+| 2300006 | Couldn't resolve host name. |
+| 2300007 | Couldn't connect to server. |
+| 2300008 | Weird server reply. |
+| 2300009 | Access denied to remote resource. |
+| 2300016 | Error in the HTTP2 framing layer. |
+| 2300018 | Transferred a partial file. |
+| 2300023 | Failed writing received data to disk/application. |
+| 2300025 | Upload failed. |
+| 2300026 | Failed to open/read local data from file/application. |
+| 2300027 | Out of memory. |
+| 2300028 | Timeout was reached. |
+| 2300047 | Number of redirects hit maximum amount. |
+| 2300052 | Server returned nothing (no headers, no data). |
+| 2300055 | Failed sending data to the peer. |
+| 2300056 | Failure when receiving data from the peer. |
+| 2300058 | Problem with the local SSL certificate. |
+| 2300059 | Couldn't use specified SSL cipher. |
+| 2300060 | SSL peer certificate or SSH remote key was not OK. |
+| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.|
+| 2300063 | Maximum file size exceeded. |
+| 2300070 | Disk full or allocation exceeded. |
+| 2300073 | Remote file already exists. |
+| 2300077 | Problem with the SSL CA cert (path? access rights?). |
+| 2300078 | Remote file not found. |
+| 2300094 | An authentication function returned an error. |
+| 2300999 | Unknown Other Error. |
+
+>**错误码说明:**
+> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
+> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
+
**示例:**
```js
@@ -163,7 +223,7 @@ httpRequest.request("EXAMPLE_URL",
### request
-request\(url: string, options? : HttpRequestOptions\): Promise
+request\(url: string, options? : HttpRequestOptions\): Promise\
根据URL地址,发起HTTP网络请求,使用Promise方式作为异步方法。
@@ -184,6 +244,45 @@ request\(url: string, options? : HttpRequestOptions\): Promise
| :------------------------------------- | :-------------------------------- |
| Promise<[HttpResponse](#httpresponse)> | 以Promise形式返回发起请求的结果。 |
+**错误码:**
+
+| 错误码ID | 错误信息 |
+|---------|-------------------------------------------------------|
+| 401 | Parameter error. |
+| 201 | Permission denied. |
+| 2300001 | Unsupported protocol. |
+| 2300003 | URL using bad/illegal format or missing URL. |
+| 2300005 | Couldn't resolve proxy name. |
+| 2300006 | Couldn't resolve host name. |
+| 2300007 | Couldn't connect to server. |
+| 2300008 | Weird server reply. |
+| 2300009 | Access denied to remote resource. |
+| 2300016 | Error in the HTTP2 framing layer. |
+| 2300018 | Transferred a partial file. |
+| 2300023 | Failed writing received data to disk/application. |
+| 2300025 | Upload failed. |
+| 2300026 | Failed to open/read local data from file/application. |
+| 2300027 | Out of memory. |
+| 2300028 | Timeout was reached. |
+| 2300047 | Number of redirects hit maximum amount. |
+| 2300052 | Server returned nothing (no headers, no data). |
+| 2300055 | Failed sending data to the peer. |
+| 2300056 | Failure when receiving data from the peer. |
+| 2300058 | Problem with the local SSL certificate. |
+| 2300059 | Couldn't use specified SSL cipher. |
+| 2300060 | SSL peer certificate or SSH remote key was not OK. |
+| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.|
+| 2300063 | Maximum file size exceeded. |
+| 2300070 | Disk full or allocation exceeded. |
+| 2300073 | Remote file already exists. |
+| 2300077 | Problem with the SSL CA cert (path? access rights?). |
+| 2300078 | Remote file not found. |
+| 2300094 | An authentication function returned an error. |
+| 2300999 | Unknown Other Error. |
+
+>**错误码说明:**
+> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
+> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
**示例:**
@@ -222,6 +321,209 @@ destroy\(\): void
httpRequest.destroy();
```
+### request210+
+
+request2(url: string, callback: AsyncCallback): void
+
+根据URL地址和相关配置项,发起HTTP网络请求并返回流式响应,使用callback方式作为异步方法。
+
+**需要权限**:ohos.permission.INTERNET
+
+**系统能力**:SystemCapability.Communication.NetStack
+
+**参数:**
+
+| 参数名 | 类型 | 必填 | 说明 |
+| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- |
+| url | string | 是 | 发起网络请求的URL地址。 |
+| callback | AsyncCallback\ | 是 | 回调函数。 |
+
+**错误码:**
+
+| 错误码ID | 错误信息 |
+|---------|-------------------------------------------------------|
+| 401 | Parameter error. |
+| 201 | Permission denied. |
+| 2300003 | URL using bad/illegal format or missing URL. |
+| 2300007 | Couldn't connect to server. |
+| 2300028 | Timeout was reached. |
+| 2300052 | Server returned nothing (no headers, no data). |
+| 2300999 | Unknown Other Error. |
+
+>**错误码说明:**
+> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
+> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
+
+**示例:**
+
+```js
+httpRequest.request2("EXAMPLE_URL", (err) => {
+ if (!err) {
+ console.info(request2 OK!);
+ } else {
+ console.info("request2 ERROR : err = " + JSON.stringify(err));
+ }
+})
+```
+
+### request210+
+
+request2(url: string, options: HttpRequestOptions, callback: AsyncCallback): void
+
+根据URL地址和相关配置项,发起HTTP网络请求并返回流式响应,使用callback方式作为异步方法。
+
+**需要权限**:ohos.permission.INTERNET
+
+**系统能力**:SystemCapability.Communication.NetStack
+
+**参数:**
+
+| 参数名 | 类型 | 必填 | 说明 |
+| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- |
+| url | string | 是 | 发起网络请求的URL地址。 |
+| options | HttpRequestOptions | 是 | 参考[HttpRequestOptions](#httprequestoptions)。 |
+| callback | AsyncCallback\ | 是 | 回调函数。 |
+
+**错误码:**
+
+| 错误码ID | 错误信息 |
+|---------|-------------------------------------------------------|
+| 401 | Parameter error. |
+| 201 | Permission denied. |
+| 2300001 | Unsupported protocol. |
+| 2300003 | URL using bad/illegal format or missing URL. |
+| 2300005 | Couldn't resolve proxy name. |
+| 2300006 | Couldn't resolve host name. |
+| 2300007 | Couldn't connect to server. |
+| 2300008 | Weird server reply. |
+| 2300009 | Access denied to remote resource. |
+| 2300016 | Error in the HTTP2 framing layer. |
+| 2300018 | Transferred a partial file. |
+| 2300023 | Failed writing received data to disk/application. |
+| 2300025 | Upload failed. |
+| 2300026 | Failed to open/read local data from file/application. |
+| 2300027 | Out of memory. |
+| 2300028 | Timeout was reached. |
+| 2300047 | Number of redirects hit maximum amount. |
+| 2300052 | Server returned nothing (no headers, no data). |
+| 2300055 | Failed sending data to the peer. |
+| 2300056 | Failure when receiving data from the peer. |
+| 2300058 | Problem with the local SSL certificate. |
+| 2300059 | Couldn't use specified SSL cipher. |
+| 2300060 | SSL peer certificate or SSH remote key was not OK. |
+| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.|
+| 2300063 | Maximum file size exceeded. |
+| 2300070 | Disk full or allocation exceeded. |
+| 2300073 | Remote file already exists. |
+| 2300077 | Problem with the SSL CA cert (path? access rights?). |
+| 2300078 | Remote file not found. |
+| 2300094 | An authentication function returned an error. |
+| 2300999 | Unknown Other Error. |
+
+>**错误码说明:**
+> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
+> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
+
+**示例:**
+
+```js
+httpRequest.request2("EXAMPLE_URL",
+{
+ method: http.RequestMethod.GET,
+ header: {
+ 'Content-Type': 'application/json'
+ },
+ readTimeout: 60000,
+ connectTimeout: 60000
+}, (err) => {
+ if (!err) {
+ console.info(request2 OK!);
+ } else {
+ console.info("request2 ERROR : err = " + JSON.stringify(err));
+ }
+})
+```
+### request210+
+
+request2\(url: string, options? : HttpRequestOptions\): Promise\
+
+根据URL地址,发起HTTP网络请求并返回流式响应,使用Promise方式作为异步方法。
+
+**需要权限**:ohos.permission.INTERNET
+
+**系统能力**:SystemCapability.Communication.NetStack
+
+**参数:**
+
+| 参数名 | 类型 | 必填 | 说明 |
+| ------- | ------------------ | ---- | ----------------------------------------------- |
+| url | string | 是 | 发起网络请求的URL地址。 |
+| options | HttpRequestOptions | 否 | 参考[HttpRequestOptions](#httprequestoptions)。 |
+
+**返回值:**
+
+| 类型 | 说明 |
+| :------------------------------------- | :-------------------------------- |
+| Promise\ | 以Promise形式返回发起请求的结果。 |
+
+**错误码:**
+
+| 错误码ID | 错误信息 |
+|---------|-------------------------------------------------------|
+| 401 | Parameter error. |
+| 201 | Permission denied. |
+| 2300001 | Unsupported protocol. |
+| 2300003 | URL using bad/illegal format or missing URL. |
+| 2300005 | Couldn't resolve proxy name. |
+| 2300006 | Couldn't resolve host name. |
+| 2300007 | Couldn't connect to server. |
+| 2300008 | Weird server reply. |
+| 2300009 | Access denied to remote resource. |
+| 2300016 | Error in the HTTP2 framing layer. |
+| 2300018 | Transferred a partial file. |
+| 2300023 | Failed writing received data to disk/application. |
+| 2300025 | Upload failed. |
+| 2300026 | Failed to open/read local data from file/application. |
+| 2300027 | Out of memory. |
+| 2300028 | Timeout was reached. |
+| 2300047 | Number of redirects hit maximum amount. |
+| 2300052 | Server returned nothing (no headers, no data). |
+| 2300055 | Failed sending data to the peer. |
+| 2300056 | Failure when receiving data from the peer. |
+| 2300058 | Problem with the local SSL certificate. |
+| 2300059 | Couldn't use specified SSL cipher. |
+| 2300060 | SSL peer certificate or SSH remote key was not OK. |
+| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.|
+| 2300063 | Maximum file size exceeded. |
+| 2300070 | Disk full or allocation exceeded. |
+| 2300073 | Remote file already exists. |
+| 2300077 | Problem with the SSL CA cert (path? access rights?). |
+| 2300078 | Remote file not found. |
+| 2300094 | An authentication function returned an error. |
+| 2300999 | Unknown Other Error. |
+
+>**错误码说明:**
+> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
+> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:
+
+**示例:**
+
+```js
+let promise = httpRequest.request("EXAMPLE_URL", {
+ method: http.RequestMethod.GET,
+ connectTimeout: 60000,
+ readTimeout: 60000,
+ header: {
+ 'Content-Type': 'application/json'
+ }
+});
+promise.then(() => {
+ console.info(request2 OK!);
+}).catch((err) => {
+ console.info("request2 ERROR : err = " + JSON.stringify(err));
+});
+```
+
### on\('headerReceive'\)
on\(type: 'headerReceive', callback: AsyncCallback