1# @ohos.net.http (数据请求) 2 3本模块提供HTTP数据请求能力。应用可以通过HTTP发起一个数据请求,支持常见的GET、POST、OPTIONS、HEAD、PUT、DELETE、TRACE、CONNECT方法。 4 5> **说明:** 6> 7> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> **建议使用[Remote Communication Kit](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/remote-communication-kit-guide-V5)进行HTTP请求,Remote Communication Kit将持续演进。** 10## 导入模块 11 12```ts 13import { http } from '@kit.NetworkKit'; 14``` 15 16## 完整示例 17 18```ts 19// 引入包名 20import { http } from '@kit.NetworkKit'; 21import { BusinessError } from '@kit.BasicServicesKit'; 22 23// 每一个httpRequest对应一个HTTP请求任务,不可复用 24let httpRequest = http.createHttp(); 25// 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息 26// 从API 8开始,使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback)。 8+ 27httpRequest.on('headersReceive', (header: Object) => { 28 console.info('header: ' + JSON.stringify(header)); 29}); 30 31httpRequest.request(// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定 32 "EXAMPLE_URL", 33 { 34 method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET 35 // 当使用POST请求时此字段用于传递请求体内容,具体格式与服务端协商确定 36 extraData: 'data to send', 37 expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型 38 usingCache: true, // 可选,默认为true 39 priority: 1, // 可选,默认为1 40 // 开发者根据自身业务需要添加header字段 41 header: { 'Accept' : 'application/json' }, 42 readTimeout: 60000, // 可选,默认为60000ms 43 connectTimeout: 60000, // 可选,默认为60000ms 44 usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定 45 usingProxy: false, //可选,默认不使用网络代理,自API 10开始支持该属性 46 caPath: '/path/to/cacert.pem', // 可选,默认使用系统预设CA证书,自API 10开始支持该属性 47 clientCert: { // 可选,默认不使用客户端证书,自API 11开始支持该属性 48 certPath: '/path/to/client.pem', // 默认不使用客户端证书,自API 11开始支持该属性 49 keyPath: '/path/to/client.key', // 若证书包含Key信息,传入空字符串,自API 11开始支持该属性 50 certType: http.CertType.PEM, // 可选,默认使用PEM,自API 11开始支持该属性 51 keyPassword: "passwordToKey" // 可选,输入key文件的密码,自API 11开始支持该属性 52 }, 53 certificatePinning: [ // 可选,支持证书锁定配置信息的动态设置,自API 12开始支持该属性 54 { 55 publicKeyHash: 'Pin1', // 由应用传入的证书PIN码,自API 12开始支持该属性 56 hashAlgorithm: 'SHA-256' // 加密算法,当前仅支持SHA-256,自API 12开始支持该属性 57 }, { 58 publicKeyHash: 'Pin2', // 由应用传入的证书PIN码,自API 12开始支持该属性 59 hashAlgorithm: 'SHA-256' // 加密算法,当前仅支持SHA-256,自API 12开始支持该属性 60 } 61 ], 62 multiFormDataList: [ // 可选,仅当Header中,'content-Type'为'multipart/form-data'时生效,自API 11开始支持该属性 63 { 64 name: "Part1", // 数据名,自API 11开始支持该属性 65 contentType: 'text/plain', // 数据类型,自API 11开始支持该属性 66 data: 'Example data', // 可选,数据内容,自API 11开始支持该属性 67 remoteFileName: 'example.txt' // 可选,自API 11开始支持该属性 68 }, { 69 name: "Part2", // 数据名,自API 11开始支持该属性 70 contentType: 'text/plain', // 数据类型,自API 11开始支持该属性 71 // data/app/el2/100/base/com.example.myapplication/haps/entry/files/fileName.txt 72 filePath: `${getContext(this).filesDir}/fileName.txt`, // 可选,传入文件路径,自API 11开始支持该属性 73 remoteFileName: 'fileName.txt' // 可选,自API 11开始支持该属性 74 } 75 ] 76 }, 77 (err: BusinessError, data: http.HttpResponse) => { 78 if (!err) { 79 // data.result为HTTP响应内容,可根据业务需要进行解析 80 console.info('Result:' + JSON.stringify(data.result)); 81 console.info('code:' + JSON.stringify(data.responseCode)); 82 console.info('type:' + JSON.stringify(data.resultType)); 83 // data.header为HTTP响应头,可根据业务需要进行解析 84 console.info('header:' + JSON.stringify(data.header)); 85 console.info('cookies:' + JSON.stringify(data.cookies)); // 自API version 8开始支持cookie 86 // 取消订阅HTTP响应头事件 87 httpRequest.off('headersReceive'); 88 // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。 89 httpRequest.destroy(); 90 } else { 91 console.info('error:' + JSON.stringify(err)); 92 // 取消订阅HTTP响应头事件 93 httpRequest.off('headersReceive'); 94 // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。 95 httpRequest.destroy(); 96 } 97 }); 98``` 99 100> **说明:** 101> console.info()输出的数据中包含换行符会导致数据出现截断现象。 102> 103> 自API 12开始支持接收经过brotli算法压缩的HTTP响应。 104 105## http.createHttp 106 107createHttp(): HttpRequest 108 109创建一个HTTP请求,里面包括发起请求、中断请求、订阅/取消订阅HTTP Response Header事件。每一个HttpRequest对象对应一个HTTP请求。如需发起多个HTTP请求,须为每个HTTP请求创建对应HttpRequest对象。 110 111> **说明:** 112> 当该请求使用完毕时,须调用destroy方法主动销毁HttpRequest对象。 113 114**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 115 116**系统能力**:SystemCapability.Communication.NetStack 117 118**返回值:** 119 120| 类型 | 说明 | 121| :---------- | :----------------------------------------------------------- | 122| HttpRequest | 返回一个HttpRequest对象,里面包括request、requestInStream、destroy、on和off方法。 | 123 124**示例:** 125 126```ts 127import { http } from '@kit.NetworkKit'; 128 129let httpRequest = http.createHttp(); 130``` 131 132## HttpRequest 133 134HTTP请求任务。在调用HttpRequest的方法前,需要先通过createHttp()创建一个任务。 135 136### request 137 138request(url: string, callback: AsyncCallback\<HttpResponse\>): void 139 140根据URL地址,发起HTTP网络请求,使用callback方式作为异步方法。 141 142> **说明:** 143> 此接口仅支持数据大小为5M以内的数据接收。 144> 若url包含中文或其他语言,需先调用encodeURL(url)编码,再发起请求。 145 146**需要权限**:ohos.permission.INTERNET 147 148**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 149 150**系统能力**:SystemCapability.Communication.NetStack 151 152**参数:** 153 154| 参数名 | 类型 | 必填 | 说明 | 155| -------- | ---------------------------------------------- | ---- | ---------------------- | 156| url | string | 是 | 发起网络请求的URL地址。 | 157| callback | AsyncCallback\<[HttpResponse](#httpresponse)\> | 是 | 回调函数。 | 158 159**错误码:** 160 161| 错误码ID | 错误信息 | 162|---------|----------------------------------------------------------------| 163| 401 | Parameter error. | 164| 201 | Permission denied. | 165| 2300001 | Unsupported protocol. | 166| 2300003 | Invalid URL format or missing URL. | 167| 2300005 | Failed to resolve the proxy name. | 168| 2300006 | Failed to resolve the host name. | 169| 2300007 | Failed to connect to the server. | 170| 2300008 | Invalid server response. | 171| 2300009 | Access to the remote resource denied. | 172| 2300016 | Error in the HTTP2 framing layer. | 173| 2300018 | Transferred a partial file. | 174| 2300023 | Failed to write the received data to the disk or application. | 175| 2300025 | Upload failed. | 176| 2300026 | Failed to open or read local data from the file or application.| 177| 2300027 | Out of memory. | 178| 2300028 | Operation timeout. | 179| 2300047 | The number of redirections reaches the maximum allowed. | 180| 2300052 | The server returned nothing (no header or data). | 181| 2300055 | Failed to send data to the peer. | 182| 2300056 | Failed to receive data from the peer. | 183| 2300058 | Local SSL certificate error. | 184| 2300059 | The specified SSL cipher cannot be used. | 185| 2300060 | Invalid SSL peer certificate or SSH remote key. | 186| 2300061 | Invalid HTTP encoding format. | 187| 2300063 | Maximum file size exceeded. | 188| 2300070 | Remote disk full. | 189| 2300073 | Remote file already exists. | 190| 2300077 | The SSL CA certificate does not exist or is inaccessible. | 191| 2300078 | Remote file not found. | 192| 2300094 | Authentication error. | 193| 2300998 | It is not allowed to access this domain. | 194| 2300999 | Unknown error. | 195 196> **错误码说明:** 197> 以上错误码的详细介绍参见[通用错误码](../errorcode-universal.md)和[HTTP错误码](errorcode-net-http.md)。 198> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 199 200**示例:** 201 202```ts 203import { http } from '@kit.NetworkKit'; 204 205let httpRequest = http.createHttp(); 206httpRequest.request("EXAMPLE_URL", (err: Error, data: http.HttpResponse) => { 207 if (!err) { 208 console.info('Result:' + data.result); 209 console.info('code:' + data.responseCode); 210 console.info('type:' + JSON.stringify(data.resultType)); 211 console.info('header:' + JSON.stringify(data.header)); 212 console.info('cookies:' + data.cookies); // 自API version 8开始支持cookie 213 } else { 214 console.info('error:' + JSON.stringify(err)); 215 } 216}); 217``` 218 219### request 220 221request(url: string, options: HttpRequestOptions, callback: AsyncCallback\<HttpResponse\>):void 222 223根据URL地址和相关配置项,发起HTTP网络请求,使用callback方式作为异步方法。 224 225> **说明:** 226> 此接口仅支持数据大小为5M以内的数据接收。 227> 228> 如需传入cookies,请开发者自行在参数options中添加。 229 230**需要权限**:ohos.permission.INTERNET 231 232**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 233 234**系统能力**:SystemCapability.Communication.NetStack 235 236**参数:** 237 238| 参数名 | 类型 | 必填 | 说明 | 239| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- | 240| url | string | 是 | 发起网络请求的URL地址。 | 241| options | HttpRequestOptions | 是 | 参考[HttpRequestOptions](#httprequestoptions)。 | 242| callback | AsyncCallback\<[HttpResponse](#httpresponse)\> | 是 | 回调函数。 | 243 244**错误码:** 245 246| 错误码ID | 错误信息 | 247|---------|----------------------------------------------------------------| 248| 401 | Parameter error. | 249| 201 | Permission denied. | 250| 2300001 | Unsupported protocol. | 251| 2300003 | Invalid URL format or missing URL. | 252| 2300005 | Failed to resolve the proxy name. | 253| 2300006 | Failed to resolve the host name. | 254| 2300007 | Failed to connect to the server. | 255| 2300008 | Invalid server response. | 256| 2300009 | Access to the remote resource denied. | 257| 2300016 | Error in the HTTP2 framing layer. | 258| 2300018 | Transferred a partial file. | 259| 2300023 | Failed to write the received data to the disk or application. | 260| 2300025 | Upload failed. | 261| 2300026 | Failed to open or read local data from the file or application.| 262| 2300027 | Out of memory. | 263| 2300028 | Operation timeout. | 264| 2300047 | The number of redirections reaches the maximum allowed. | 265| 2300052 | The server returned nothing (no header or data). | 266| 2300055 | Failed to send data to the peer. | 267| 2300056 | Failed to receive data from the peer. | 268| 2300058 | Local SSL certificate error. | 269| 2300059 | The specified SSL cipher cannot be used. | 270| 2300060 | Invalid SSL peer certificate or SSH remote key. | 271| 2300061 | Invalid HTTP encoding format. | 272| 2300063 | Maximum file size exceeded. | 273| 2300070 | Remote disk full. | 274| 2300073 | Remote file already exists. | 275| 2300077 | The SSL CA certificate does not exist or is inaccessible. | 276| 2300078 | Remote file not found. | 277| 2300094 | Authentication error. | 278| 2300998 | It is not allowed to access this domain. | 279| 2300999 | Unknown error. | 280 281> **错误码说明:** 282> 以上错误码的详细介绍参见[通用错误码](../errorcode-universal.md)和[HTTP错误码](errorcode-net-http.md)。 283> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 284 285**示例:** 286 287```ts 288import { http } from '@kit.NetworkKit'; 289 290class Header { 291 public contentType: string; 292 293 constructor(contentType: string) { 294 this.contentType = contentType; 295 } 296} 297 298let httpRequest = http.createHttp(); 299let options: http.HttpRequestOptions = { 300 method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET 301 // 当使用POST请求时此字段用于传递请求体内容,具体格式与服务端协商确定 302 extraData: 'data to send', 303 expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型 304 usingCache: true, // 可选,默认为true 305 priority: 1, // 可选,默认为1 306 // 开发者根据自身业务需要添加header字段 307 header: new Header('application/json'), 308 readTimeout: 60000, // 可选,默认为60000ms 309 connectTimeout: 60000, // 可选,默认为60000ms 310 usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定 311 usingProxy: false, //可选,默认不使用网络代理,自API 10开始支持该属性 312}; 313 314httpRequest.request("EXAMPLE_URL", options, (err: Error, data: http.HttpResponse) => { 315 if (!err) { 316 console.info('Result:' + data.result); 317 console.info('code:' + data.responseCode); 318 console.info('type:' + JSON.stringify(data.resultType)); 319 console.info('header:' + JSON.stringify(data.header)); 320 console.info('cookies:' + data.cookies); // 自API version 8开始支持cookie 321 } else { 322 console.info('error:' + JSON.stringify(err)); 323 } 324}); 325``` 326 327### request 328 329request(url: string, options? : HttpRequestOptions): Promise\<HttpResponse\> 330 331根据URL地址,发起HTTP网络请求,使用Promise方式作为异步方法。 332 333> **说明:** 334> 此接口仅支持数据大小为5M以内的数据接收。 335> 336> 如需传入cookies,请开发者自行在参数options中添加。 337 338**需要权限**:ohos.permission.INTERNET 339 340**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 341 342**系统能力**:SystemCapability.Communication.NetStack 343 344**参数:** 345 346| 参数名 | 类型 | 必填 | 说明 | 347| ------- | ------------------ | ---- | ----------------------------------------------- | 348| url | string | 是 | 发起网络请求的URL地址。 | 349| options | HttpRequestOptions | 否 | 参考[HttpRequestOptions](#httprequestoptions)。 | 350 351**返回值:** 352 353| 类型 | 说明 | 354| :------------------------------------- | :-------------------------------- | 355| Promise<[HttpResponse](#httpresponse)> | 以Promise形式返回发起请求的结果。 | 356 357**错误码:** 358 359| 错误码ID | 错误信息 | 360|---------|----------------------------------------------------------------| 361| 401 | Parameter error. | 362| 201 | Permission denied. | 363| 2300001 | Unsupported protocol. | 364| 2300003 | Invalid URL format or missing URL. | 365| 2300005 | Failed to resolve the proxy name. | 366| 2300006 | Failed to resolve the host name. | 367| 2300007 | Failed to connect to the server. | 368| 2300008 | Invalid server response. | 369| 2300009 | Access to the remote resource denied. | 370| 2300016 | Error in the HTTP2 framing layer. | 371| 2300018 | Transferred a partial file. | 372| 2300023 | Failed to write the received data to the disk or application. | 373| 2300025 | Upload failed. | 374| 2300026 | Failed to open or read local data from the file or application.| 375| 2300027 | Out of memory. | 376| 2300028 | Operation timeout. | 377| 2300047 | The number of redirections reaches the maximum allowed. | 378| 2300052 | The server returned nothing (no header or data). | 379| 2300055 | Failed to send data to the peer. | 380| 2300056 | Failed to receive data from the peer. | 381| 2300058 | Local SSL certificate error. | 382| 2300059 | The specified SSL cipher cannot be used. | 383| 2300060 | Invalid SSL peer certificate or SSH remote key. | 384| 2300061 | Invalid HTTP encoding format. | 385| 2300063 | Maximum file size exceeded. | 386| 2300070 | Remote disk full. | 387| 2300073 | Remote file already exists. | 388| 2300077 | The SSL CA certificate does not exist or is inaccessible. | 389| 2300078 | Remote file not found. | 390| 2300094 | Authentication error. | 391| 2300998 | It is not allowed to access this domain. | 392| 2300999 | Unknown error. | 393 394> **错误码说明:** 395> 以上错误码的详细介绍参见[通用错误码](../errorcode-universal.md)和[HTTP错误码](errorcode-net-http.md)。 396> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 397 398**示例:** 399 400```ts 401import { http } from '@kit.NetworkKit'; 402 403class Header { 404 public contentType: string; 405 406 constructor(contentType: string) { 407 this.contentType = contentType; 408 } 409} 410 411let httpRequest = http.createHttp(); 412let promise = httpRequest.request("EXAMPLE_URL", { 413 method: http.RequestMethod.GET, 414 connectTimeout: 60000, 415 readTimeout: 60000, 416 header: new Header('application/json') 417}); 418promise.then((data:http.HttpResponse) => { 419 console.info('Result:' + data.result); 420 console.info('code:' + data.responseCode); 421 console.info('type:' + JSON.stringify(data.resultType)); 422 console.info('header:' + JSON.stringify(data.header)); 423 console.info('cookies:' + data.cookies); // 自API version 8开始支持cookie 424 console.info('header.content-Type:' + data.header); 425 console.info('header.Status-Line:' + data.header); 426}).catch((err:Error) => { 427 console.info('error:' + JSON.stringify(err)); 428}); 429``` 430 431### destroy 432 433destroy(): void 434 435中断请求任务。 436 437**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 438 439**系统能力**:SystemCapability.Communication.NetStack 440 441**示例:** 442 443```ts 444import { http } from '@kit.NetworkKit'; 445let httpRequest = http.createHttp(); 446 447httpRequest.destroy(); 448``` 449 450### requestInStream<sup>10+</sup> 451 452requestInStream(url: string, callback: AsyncCallback\<number\>): void 453 454根据URL地址,发起HTTP网络请求并返回流式响应,使用callback方式作为异步方法。 455 456**需要权限**:ohos.permission.INTERNET 457 458**系统能力**:SystemCapability.Communication.NetStack 459 460**参数:** 461 462| 参数名 | 类型 | 必填 | 说明 | 463| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- | 464| url | string | 是 | 发起网络请求的URL地址。 | 465| callback | AsyncCallback\<number\> | 是 | 回调函数。 | 466 467**错误码:** 468 469| 错误码ID | 错误信息 | 470|---------|----------------------------------------------------------------| 471| 401 | Parameter error. | 472| 201 | Permission denied. | 473| 2300001 | Unsupported protocol. | 474| 2300003 | Invalid URL format or missing URL. | 475| 2300005 | Failed to resolve the proxy name. | 476| 2300006 | Failed to resolve the host name. | 477| 2300007 | Failed to connect to the server. | 478| 2300008 | Invalid server response. | 479| 2300009 | Access to the remote resource denied. | 480| 2300016 | Error in the HTTP2 framing layer. | 481| 2300018 | Transferred a partial file. | 482| 2300023 | Failed to write the received data to the disk or application. | 483| 2300025 | Upload failed. | 484| 2300026 | Failed to open or read local data from the file or application.| 485| 2300027 | Out of memory. | 486| 2300028 | Operation timeout. | 487| 2300047 | The number of redirections reaches the maximum allowed. | 488| 2300052 | The server returned nothing (no header or data). | 489| 2300055 | Failed to send data to the peer. | 490| 2300056 | Failed to receive data from the peer. | 491| 2300058 | Local SSL certificate error. | 492| 2300059 | The specified SSL cipher cannot be used. | 493| 2300060 | Invalid SSL peer certificate or SSH remote key. | 494| 2300061 | Invalid HTTP encoding format. | 495| 2300063 | Maximum file size exceeded. | 496| 2300070 | Remote disk full. | 497| 2300073 | Remote file already exists. | 498| 2300077 | The SSL CA certificate does not exist or is inaccessible. | 499| 2300078 | Remote file not found. | 500| 2300094 | Authentication error. | 501| 2300998 | It is not allowed to access this domain. | 502| 2300999 | Unknown error. | 503 504> **错误码说明:** 505> 以上错误码的详细介绍参见[通用错误码](../errorcode-universal.md)和[HTTP错误码](errorcode-net-http.md)。 506> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 507 508**示例:** 509 510```ts 511import { http } from '@kit.NetworkKit'; 512import { BusinessError } from '@kit.BasicServicesKit'; 513 514let httpRequest = http.createHttp(); 515httpRequest.requestInStream("EXAMPLE_URL", (err: BusinessError, data: number) => { 516 if (!err) { 517 console.info("requestInStream OK! ResponseCode is " + JSON.stringify(data)); 518 } else { 519 console.info("requestInStream ERROR : err = " + JSON.stringify(err)); 520 } 521}) 522``` 523 524### requestInStream<sup>10+</sup> 525 526requestInStream(url: string, options: HttpRequestOptions, callback: AsyncCallback\<number\>): void 527 528根据URL地址和相关配置项,发起HTTP网络请求并返回流式响应,使用callback方式作为异步方法。 529 530**需要权限**:ohos.permission.INTERNET 531 532**系统能力**:SystemCapability.Communication.NetStack 533 534**参数:** 535 536| 参数名 | 类型 | 必填 | 说明 | 537| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- | 538| url | string | 是 | 发起网络请求的URL地址。 | 539| options | HttpRequestOptions | 是 | 参考[HttpRequestOptions](#httprequestoptions)。 | 540| callback | AsyncCallback\<[number](#responsecode)\> | 是 | 回调函数。 | 541 542**错误码:** 543 544| 错误码ID | 错误信息 | 545|---------|----------------------------------------------------------------| 546| 401 | Parameter error. | 547| 201 | Permission denied. | 548| 2300001 | Unsupported protocol. | 549| 2300003 | Invalid URL format or missing URL. | 550| 2300005 | Failed to resolve the proxy name. | 551| 2300006 | Failed to resolve the host name. | 552| 2300007 | Failed to connect to the server. | 553| 2300008 | Invalid server response. | 554| 2300009 | Access to the remote resource denied. | 555| 2300016 | Error in the HTTP2 framing layer. | 556| 2300018 | Transferred a partial file. | 557| 2300023 | Failed to write the received data to the disk or application. | 558| 2300025 | Upload failed. | 559| 2300026 | Failed to open or read local data from the file or application.| 560| 2300027 | Out of memory. | 561| 2300028 | Operation timeout. | 562| 2300047 | The number of redirections reaches the maximum allowed. | 563| 2300052 | The server returned nothing (no header or data). | 564| 2300055 | Failed to send data to the peer. | 565| 2300056 | Failed to receive data from the peer. | 566| 2300058 | Local SSL certificate error. | 567| 2300059 | The specified SSL cipher cannot be used. | 568| 2300060 | Invalid SSL peer certificate or SSH remote key. | 569| 2300061 | Invalid HTTP encoding format. | 570| 2300063 | Maximum file size exceeded. | 571| 2300070 | Remote disk full. | 572| 2300073 | Remote file already exists. | 573| 2300077 | The SSL CA certificate does not exist or is inaccessible. | 574| 2300078 | Remote file not found. | 575| 2300094 | Authentication error. | 576| 2300998 | It is not allowed to access this domain. | 577| 2300999 | Unknown error. | 578 579> **错误码说明:** 580> 以上错误码的详细介绍参见[通用错误码](../errorcode-universal.md)和[HTTP错误码](errorcode-net-http.md)。 581> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 582 583**示例:** 584 585```ts 586import { http } from '@kit.NetworkKit'; 587import { BusinessError } from '@kit.BasicServicesKit'; 588 589class Header { 590 public contentType: string; 591 592 constructor(contentType: string) { 593 this.contentType = contentType; 594 } 595} 596 597let httpRequest = http.createHttp(); 598let options: http.HttpRequestOptions = { 599 method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET 600 // 当使用POST请求时此字段用于传递请求体内容,具体格式与服务端协商确定 601 extraData: 'data to send', 602 expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型 603 usingCache: true, // 可选,默认为true 604 priority: 1, // 可选,默认为1 605 // 开发者根据自身业务需要添加header字段 606 header: new Header('application/json'), 607 readTimeout: 60000, // 可选,默认为60000ms 608 connectTimeout: 60000, // 可选,默认为60000ms 609 usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定 610 usingProxy: false, //可选,默认不使用网络代理,自API 10开始支持该属性 611}; 612httpRequest.requestInStream("EXAMPLE_URL", options, (err: BusinessError<void> , data: number) => { 613 if (!err) { 614 console.info("requestInStream OK! ResponseCode is " + JSON.stringify(data)); 615 } else { 616 console.info("requestInStream ERROR : err = " + JSON.stringify(err)); 617 } 618}) 619``` 620 621### requestInStream<sup>10+</sup> 622 623requestInStream(url: string, options? : HttpRequestOptions): Promise\<number\> 624 625根据URL地址,发起HTTP网络请求并返回流式响应,使用Promise方式作为异步方法。 626 627**需要权限**:ohos.permission.INTERNET 628 629**系统能力**:SystemCapability.Communication.NetStack 630 631**参数:** 632 633| 参数名 | 类型 | 必填 | 说明 | 634| ------- | ------------------ | ---- | ----------------------------------------------- | 635| url | string | 是 | 发起网络请求的URL地址。 | 636| options | HttpRequestOptions | 否 | 参考[HttpRequestOptions](#httprequestoptions)。 | 637 638**返回值:** 639 640| 类型 | 说明 | 641| :------------------------------------- | :-------------------------------- | 642| Promise\<[number](#responsecode)\> | 以Promise形式返回发起请求的结果。 | 643 644**错误码:** 645 646| 错误码ID | 错误信息 | 647|---------|----------------------------------------------------------------| 648| 401 | Parameter error. | 649| 201 | Permission denied. | 650| 2300001 | Unsupported protocol. | 651| 2300003 | Invalid URL format or missing URL. | 652| 2300005 | Failed to resolve the proxy name. | 653| 2300006 | Failed to resolve the host name. | 654| 2300007 | Failed to connect to the server. | 655| 2300008 | Invalid server response. | 656| 2300009 | Access to the remote resource denied. | 657| 2300016 | Error in the HTTP2 framing layer. | 658| 2300018 | Transferred a partial file. | 659| 2300023 | Failed to write the received data to the disk or application. | 660| 2300025 | Upload failed. | 661| 2300026 | Failed to open or read local data from the file or application.| 662| 2300027 | Out of memory. | 663| 2300028 | Operation timeout. | 664| 2300047 | The number of redirections reaches the maximum allowed. | 665| 2300052 | The server returned nothing (no header or data). | 666| 2300055 | Failed to send data to the peer. | 667| 2300056 | Failed to receive data from the peer. | 668| 2300058 | Local SSL certificate error. | 669| 2300059 | The specified SSL cipher cannot be used. | 670| 2300060 | Invalid SSL peer certificate or SSH remote key. | 671| 2300061 | Invalid HTTP encoding format. | 672| 2300063 | Maximum file size exceeded. | 673| 2300070 | Remote disk full. | 674| 2300073 | Remote file already exists. | 675| 2300077 | The SSL CA certificate does not exist or is inaccessible. | 676| 2300078 | Remote file not found. | 677| 2300094 | Authentication error. | 678| 2300998 | It is not allowed to access this domain. | 679| 2300999 | Unknown error. | 680 681> **错误码说明:** 682> 以上错误码的详细介绍参见[通用错误码](../errorcode-universal.md)和[HTTP错误码](errorcode-net-http.md)。 683> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 684 685**示例:** 686 687```ts 688import { http } from '@kit.NetworkKit'; 689 690class Header { 691 public contentType: string; 692 693 constructor(contentType: string) { 694 this.contentType = contentType; 695 } 696} 697 698let httpRequest = http.createHttp(); 699let promise = httpRequest.requestInStream("EXAMPLE_URL", { 700 method: http.RequestMethod.GET, 701 connectTimeout: 60000, 702 readTimeout: 60000, 703 header: new Header('application/json') 704}); 705promise.then((data: number) => { 706 console.info("requestInStream OK!" + data); 707}).catch((err: Error) => { 708 console.info("requestInStream ERROR : err = " + JSON.stringify(err)); 709}); 710``` 711 712### on("headerReceive")<sup>(deprecated)</sup> 713 714on(type: "headerReceive", callback: AsyncCallback\<Object\>): void 715 716订阅HTTP Response Header 事件。 717 718> **说明:** 719> 此接口已废弃,建议使用[on("headersReceive")<sup>8+</sup>](#onheadersreceive8)替代。 720 721**系统能力**:SystemCapability.Communication.NetStack 722 723**参数:** 724 725| 参数名 | 类型 | 必填 | 说明 | 726| -------- | ----------------------- | ---- | --------------------------------- | 727| type | string | 是 | 订阅的事件类型,'headerReceive'。 | 728| callback | AsyncCallback\<Object\> | 是 | 回调函数。 | 729 730**示例:** 731 732```ts 733import { http } from '@kit.NetworkKit'; 734import { BusinessError } from '@kit.BasicServicesKit'; 735 736let httpRequest = http.createHttp(); 737httpRequest.on("headerReceive", (data: BusinessError) => { 738 console.info("error:" + JSON.stringify(data)); 739}); 740``` 741 742### off("headerReceive")<sup>(deprecated)</sup> 743 744off(type: "headerReceive", callback?: AsyncCallback\<Object\>): void 745 746取消订阅HTTP Response Header 事件。 747 748> **说明:** 749> 750>1. 此接口已废弃,建议使用[off("headersReceive")<sup>8+</sup>](#offheadersreceive8)替代。 751> 752>2. 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 753 754**系统能力**:SystemCapability.Communication.NetStack 755 756**参数:** 757 758| 参数名 | 类型 | 必填 | 说明 | 759| -------- | ----------------------- | ---- | ------------------------------------- | 760| type | string | 是 | 取消订阅的事件类型,'headerReceive'。 | 761| callback | AsyncCallback\<Object\> | 否 | 回调函数。 | 762 763**示例:** 764 765```ts 766import { http } from '@kit.NetworkKit'; 767 768let httpRequest = http.createHttp(); 769httpRequest.off("headerReceive"); 770``` 771 772### on("headersReceive")<sup>8+</sup> 773 774on(type: "headersReceive", callback: Callback\<Object\>): void 775 776订阅HTTP Response Header 事件。 777 778**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 779 780**系统能力**:SystemCapability.Communication.NetStack 781 782**参数:** 783 784| 参数名 | 类型 | 必填 | 说明 | 785| -------- | ------------------ | ---- | ---------------------------------- | 786| type | string | 是 | 订阅的事件类型:'headersReceive'。 | 787| callback | Callback\<Object\> | 是 | 回调函数。 | 788 789**示例:** 790 791```ts 792import { http } from '@kit.NetworkKit'; 793 794let httpRequest = http.createHttp(); 795httpRequest.on("headersReceive", (header: Object) => { 796 console.info("header: " + JSON.stringify(header)); 797}); 798httpRequest.off("headersReceive"); 799``` 800 801### off("headersReceive")<sup>8+</sup> 802 803off(type: "headersReceive", callback?: Callback\<Object\>): void 804 805取消订阅HTTP Response Header 事件。 806 807> **说明:** 808> 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 809 810**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 811 812**系统能力**:SystemCapability.Communication.NetStack 813 814**参数:** 815 816| 参数名 | 类型 | 必填 | 说明 | 817| -------- | ------------------ | ---- | -------------------------------------- | 818| type | string | 是 | 取消订阅的事件类型:'headersReceive'。 | 819| callback | Callback\<Object\> | 否 | 回调函数。 | 820 821**示例:** 822 823```ts 824import { http } from '@kit.NetworkKit'; 825 826let httpRequest = http.createHttp(); 827httpRequest.on("headersReceive", (header: Object) => { 828 console.info("header: " + JSON.stringify(header)); 829}); 830httpRequest.off("headersReceive"); 831``` 832 833### once("headersReceive")<sup>8+</sup> 834 835once(type: "headersReceive", callback: Callback\<Object\>): void 836 837订阅HTTP Response Header 事件,但是只触发一次。一旦触发之后,订阅器就会被移除。使用callback方式作为异步方法。 838 839**系统能力**:SystemCapability.Communication.NetStack 840 841**参数:** 842 843| 参数名 | 类型 | 必填 | 说明 | 844| -------- | ------------------ | ---- | ---------------------------------- | 845| type | string | 是 | 订阅的事件类型:'headersReceive'。 | 846| callback | Callback\<Object\> | 是 | 回调函数。 | 847 848**示例:** 849 850```ts 851import { http } from '@kit.NetworkKit'; 852 853let httpRequest = http.createHttp(); 854httpRequest.once("headersReceive", (header: Object) => { 855 console.info("header: " + JSON.stringify(header)); 856}); 857``` 858 859### on("dataReceive")<sup>10+</sup> 860 861on(type: "dataReceive", callback: Callback\<ArrayBuffer\>): void 862 863订阅HTTP流式响应数据接收事件。 864 865**系统能力**:SystemCapability.Communication.NetStack 866 867**参数:** 868 869| 参数名 | 类型 | 必填 | 说明 | 870| -------- | ----------------------- | ---- | --------------------------------- | 871| type | string | 是 | 订阅的事件类型,'dataReceive'。 | 872| callback | AsyncCallback\<ArrayBuffer\> | 是 | 回调函数。 | 873 874**示例:** 875 876```ts 877import { http } from '@kit.NetworkKit'; 878 879let httpRequest = http.createHttp(); 880httpRequest.on("dataReceive", (data: ArrayBuffer) => { 881 console.info("dataReceive length: " + JSON.stringify(data.byteLength)); 882}); 883httpRequest.off("dataReceive"); 884``` 885 886### off("dataReceive")<sup>10+</sup> 887 888off(type: "dataReceive", callback?: Callback\<ArrayBuffer\>): void 889 890取消订阅HTTP流式响应数据接收事件。 891 892> **说明:** 893> 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 894 895**系统能力**:SystemCapability.Communication.NetStack 896 897**参数:** 898 899| 参数名 | 类型 | 必填 | 说明 | 900| -------- | ------------------ | ---- | -------------------------------------- | 901| type | string | 是 | 取消订阅的事件类型:'dataReceive'。 | 902| callback | Callback\<ArrayBuffer\> | 否 | 回调函数。 | 903 904**示例:** 905 906```ts 907import { http } from '@kit.NetworkKit'; 908 909let httpRequest = http.createHttp(); 910httpRequest.on("dataReceive", (data: ArrayBuffer) => { 911 console.info("dataReceive length: " + JSON.stringify(data.byteLength)); 912}); 913httpRequest.off("dataReceive"); 914``` 915 916### on("dataEnd")<sup>10+</sup> 917 918on(type: "dataEnd", callback: Callback\<void\>): void 919 920订阅HTTP流式响应数据接收完毕事件。 921 922**系统能力**:SystemCapability.Communication.NetStack 923 924**参数:** 925 926| 参数名 | 类型 | 必填 | 说明 | 927| -------- | ----------------------- | ---- | --------------------------------- | 928| type | string | 是 | 订阅的事件类型,'dataEnd'。 | 929| callback | AsyncCallback\<void\> | 是 | 回调函数。 | 930 931**示例:** 932 933```ts 934import { http } from '@kit.NetworkKit'; 935 936let httpRequest = http.createHttp(); 937httpRequest.on("dataEnd", () => { 938 console.info("Receive dataEnd !"); 939}); 940httpRequest.off("dataEnd"); 941``` 942 943### off("dataEnd")<sup>10+</sup> 944 945off(type: "dataEnd", callback?: Callback\<void\>): void 946 947取消订阅HTTP流式响应数据接收完毕事件。 948 949> **说明:** 950> 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 951 952**系统能力**:SystemCapability.Communication.NetStack 953 954**参数:** 955 956| 参数名 | 类型 | 必填 | 说明 | 957| -------- | ------------------ | ---- | -------------------------------------- | 958| type | string | 是 | 取消订阅的事件类型:'dataEnd'。 | 959| callback | Callback\<void\> | 否 | 回调函数。 | 960 961**示例:** 962 963```ts 964import { http } from '@kit.NetworkKit'; 965 966let httpRequest = http.createHttp(); 967httpRequest.on("dataEnd", () => { 968 console.info("Receive dataEnd !"); 969}); 970httpRequest.off("dataEnd"); 971``` 972 973### on('dataReceiveProgress')<sup>10+</sup> 974 975on(type: 'dataReceiveProgress', callback: Callback\<DataReceiveProgressInfo\>): void 976 977订阅HTTP流式响应数据接收进度事件。 978 979**系统能力**:SystemCapability.Communication.NetStack 980 981**参数:** 982 983| 参数名 | 类型 | 必填 | 说明 | 984| -------- | ----------------------- | ---- | --------------------------------- | 985| type | string | 是 | 订阅的事件类型,'dataReceiveProgress'。 | 986| callback | AsyncCallback\<[DataReceiveProgressInfo](#datareceiveprogressinfo11)\> | 是 | 回调函数。返回数据接收进度信息。 | 987 988**示例:** 989 990```ts 991import { http } from '@kit.NetworkKit'; 992 993let httpRequest = http.createHttp(); 994httpRequest.on("dataReceiveProgress", (data: http.DataReceiveProgressInfo) => { 995 console.info("dataReceiveProgress:" + JSON.stringify(data)); 996}); 997httpRequest.off("dataReceiveProgress"); 998``` 999 1000### off('dataReceiveProgress')<sup>10+</sup> 1001 1002off(type: 'dataReceiveProgress', callback?: Callback\<DataReceiveProgressInfo\>): void 1003 1004取消订阅HTTP流式响应数据接收进度事件。 1005 1006> **说明:** 1007> 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 1008 1009**系统能力**:SystemCapability.Communication.NetStack 1010 1011**参数:** 1012 1013| 参数名 | 类型 | 必填 | 说明 | 1014| -------- | ------------------ | ---- | -------------------------------------- | 1015| type | string | 是 | 取消订阅的事件类型:'dataReceiveProgress'。 | 1016| callback | Callback\<[DataReceiveProgressInfo](#datareceiveprogressinfo11)\> | 否 | 回调函数。 返回数据接收进度信息。 | 1017 1018**示例:** 1019 1020```ts 1021import { http } from '@kit.NetworkKit'; 1022 1023let httpRequest = http.createHttp(); 1024httpRequest.on("dataReceiveProgress", (data: http.DataReceiveProgressInfo) => { 1025 console.info("dataReceiveProgress:" + JSON.stringify(data)); 1026}); 1027httpRequest.off("dataReceiveProgress"); 1028``` 1029 1030### on('dataSendProgress')<sup>11+</sup> 1031 1032on(type: 'dataSendProgress', callback: Callback\<DataSendProgressInfo\>): void 1033 1034订阅HTTP网络请求数据发送进度事件。 1035 1036**系统能力**:SystemCapability.Communication.NetStack 1037 1038**参数:** 1039 1040| 参数名 | 类型 | 必填 | 说明 | 1041| -------- | ----------------------- | ---- | --------------------------------- | 1042| type | string | 是 | 订阅的事件类型,'dataSendProgress'。 | 1043| callback | AsyncCallback\<[DataSendProgressInfo](#datasendprogressinfo11)\> | 是 | 回调函数。返回数据发送进度信息。| 1044 1045**示例:** 1046 1047```ts 1048import { http } from '@kit.NetworkKit'; 1049 1050let httpRequest = http.createHttp(); 1051httpRequest.on("dataSendProgress", (data: http.DataSendProgressInfo) => { 1052 console.info("dataSendProgress:" + JSON.stringify(data)); 1053}); 1054httpRequest.off("dataSendProgress"); 1055``` 1056 1057### off('dataSendProgress')<sup>11+</sup> 1058 1059off(type: 'dataSendProgress', callback?: Callback\<DataSendProgressInfo\>): void 1060 1061取消订阅HTTP网络请求数据发送进度事件。 1062 1063> **说明:** 1064> 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 1065 1066**系统能力**:SystemCapability.Communication.NetStack 1067 1068**参数:** 1069 1070| 参数名 | 类型 | 必填 | 说明 | 1071| -------- | ------------------ | ---- | -------------------------------------- | 1072| type | string | 是 | 取消订阅的事件类型:'dataSendProgress'。 | 1073| callback | Callback\<[DataSendProgressInfo](#datasendprogressinfo11)\> | 否 | 回调函数。返回数据接发送进度信息。 | 1074 1075**示例:** 1076 1077```ts 1078import { http } from '@kit.NetworkKit'; 1079 1080let httpRequest = http.createHttp(); 1081httpRequest.on("dataSendProgress", (data: http.DataSendProgressInfo) => { 1082 console.info("dataSendProgress:" + JSON.stringify(data)); 1083}); 1084httpRequest.off("dataSendProgress"); 1085``` 1086 1087## HttpRequestOptions 1088 1089发起请求可选参数的类型和取值范围。 1090 1091**系统能力**:SystemCapability.Communication.NetStack 1092 1093| 名称 | 类型 | 必填 | 说明 | 1094| -------------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 1095| method | [RequestMethod](#requestmethod) | 否 | 请求方式,默认为GET。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1096| extraData | string \| Object \| ArrayBuffer | 否 | 发送请求的额外数据,默认无此字段。<br />当HTTP请求为POST、PUT等方法时,此字段为HTTP请求的content,以UTF-8编码形式作为请求体。当'content-Type'为'application/x-www-form-urlencoded'时,请求提交的信息主体数据必须在key和value进行URL转码后(encodeURIComponent/encodeURI),按照键值对"key1=value1&key2=value2&key3=value3"的方式进行编码,该字段对应的类型通常为String;当'content-Type'为'text/xml'时,该字段对应的类型通常为String;当'content-Type'为'application/json'时,该字段对应的类型通常为Object;当'content-Type'为'application/octet-stream'时,该字段对应的类型通常为ArrayBuffer;当'content-Type'为'multipart/form-data'且需上传的字段为文件时,该字段对应的类型通常为ArrayBuffer。以上信息仅供参考,并可能根据具体情况有所不同。<br />- 当HTTP请求为GET、OPTIONS、DELETE、TRACE、CONNECT等方法时,此字段为HTTP请求参数的补充。开发者需传入Encode编码后的string类型参数,Object类型的参数无需预编码,参数内容会拼接到URL中进行发送;ArrayBuffer类型的参数不会做拼接处理。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1097| expectDataType<sup>9+</sup> | [HttpDataType](#httpdatatype9) | 否 | 指定返回数据的类型,默认无此字段。如果设置了此参数,系统将优先返回指定的类型。当指定其类型为Object时,最大长度为65536 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。| 1098| usingCache<sup>9+</sup> | boolean | 否 | 是否使用缓存,默认为true,请求时优先读取缓存。 缓存跟随当前进程生效。新缓存会替换旧缓存。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1099| priority<sup>9+</sup> | number | 否 | http/https请求并发优先级,值越大优先级越高,范围[1,1000],默认为1。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1100| header | Object | 否 | HTTP请求头字段。当请求方式为"POST" "PUT" "DELETE" 或者""时,默认{'content-Type': 'application/json'}, 否则默认{'content-Type': 'application/x-www-form-urlencoded'}。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1101| readTimeout | number | 否 | 读取超时时间。单位为毫秒(ms),默认为60000ms。<br />设置为0表示不会出现超时情况。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。| 1102| connectTimeout | number | 否 | 连接超时时间。单位为毫秒(ms),默认为60000ms。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1103| usingProtocol<sup>9+</sup> | [HttpProtocol](#httpprotocol9) | 否 | 使用协议。默认值由系统自动指定。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1104| usingProxy<sup>10+</sup> | boolean \| [HttpProxy](js-apis-net-connection.md#httpproxy10) | 否 | 是否使用HTTP代理,默认为false,不使用代理。<br />- 当usingProxy为布尔类型true时,使用默认网络代理。<br />- 当usingProxy为HttpProxy类型时,使用指定网络代理。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1105| caPath<sup>10+</sup> | string | 否 | 如果设置了此参数,系统将使用用户指定路径的CA证书,(开发者需保证该路径下CA证书的可访问性),否则将使用系统预设CA证书,系统预设CA证书位置:/etc/ssl/certs/cacert.pem。证书路径为沙箱映射路径(开发者可通过getContext().filesDir获取应用沙箱路径)。目前仅支持后缀名为.pem的文本格式证书。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1106| resumeFrom<sup>11+</sup> | number | 否 | 用于设置下载起始位置,该参数只能用于GET方法,不要用于其他。HTTP标准(RFC 7233第3.1节)允许服务器忽略范围请求。<br />-使用HTTP PUT时,不应使用该选项,因为该选项可能与其他选项冲突。<br />-取值范围是:1~4294967296(4GB),超出范围则不生效。 | 1107| resumeTo<sup>11+</sup> | number | 否 | 用于设置下载结束位置,该参数只能用于GET方法,不要用于其他。HTTP标准(RFC 7233第3.1节)允许服务器忽略范围请求。<br />-使用HTTP PUT时,不应使用该选项,因为该选项可能与其他选项冲突。<br />-取值范围是:1~4294967296(4GB),超出范围则不生效。 | 1108| clientCert<sup>11+</sup> | [ClientCert](#clientcert11) | 否 | 支持传输客户端证书。 | 1109| dnsOverHttps<sup>11+</sup> | string | 否 | 设置使用https协议的服务器进行DNS解析。<br />-参数必须以以下格式进行URL编码:"https:// host:port/path"。 | 1110| dnsServers<sup>11+</sup> | Array\<string\> | 否 | 设置指定的DNS服务器进行DNS解析。<br />-可以设置多个DNS解析服务器,最多3个服务器。如果有3个以上,只取前3个。<br />-服务器必须是IPV4或者IPV6地址。 | 1111| maxLimit<sup>11+</sup> | number | 否 | 响应消息的最大字节限制,默认值为5\*1024\*1024,以字节为单位。最大值为100\*1024\*1024,以字节为单位。 | 1112| multiFormDataList<sup>11+</sup> | Array<[MultiFormData](#multiformdata11)> | 否 | 当'content-Type'为'multipart/form-data'时,则上传该字段定义的数据字段表单列表。 | 1113| certificatePinning<sup>12+</sup> | [CertificatePinning](#certificatepinning12) \| CertificatePinning[] | 否 | 支持动态设置证书锁定配置,可以传入单个或多个证书PIN码。 | 1114 1115## RequestMethod 1116 1117HTTP 请求方法。 1118 1119**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1120 1121**系统能力**:SystemCapability.Communication.NetStack 1122 1123| 名称 | 值 | 说明 | 1124| :------ | ------- | :------------------ | 1125| OPTIONS | "OPTIONS" | OPTIONS方法描述了目标资源的通信选项。 | 1126| GET | "GET" | GET方法请求指定资源的表示。使用GET的请求应该只检索数据,不应该包含请求内容。 | 1127| HEAD | "HEAD" | HEAD方法请求与GET请求相同的响应,但没有响应主体。 | 1128| POST | "POST" | POST方法将实体提交给指定的资源,通常会导致服务器上的状态更改。 | 1129| PUT | "PUT" | PUT方法将目标资源的所有当前表示替换为请求内容。 | 1130| DELETE | "DELETE" | DELETE方法用于删除指定的资源。 | 1131| TRACE | "TRACE" | TRACE方法沿到达目标资源的路径执行消息环回测试。 | 1132| CONNECT | "CONNECT" | CONNECT方法建立到由目标资源标识的服务器的隧道。 | 1133 1134## ResponseCode 1135 1136发起请求返回的响应码。 1137 1138**系统能力**:SystemCapability.Communication.NetStack 1139 1140| 名称 | 值 | 说明 | 1141| ----------------- | ---- | ------------------------------------------------------------ | 1142| OK | 200 | 请求成功。一般用于GET与POST请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1143| CREATED | 201 | 已创建。成功请求并创建了新的资源。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1144| ACCEPTED | 202 | 已接受。已经接受请求,但未处理完成。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1145| NOT_AUTHORITATIVE | 203 | 非授权信息。请求成功。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1146| NO_CONTENT | 204 | 无内容。服务器成功处理,但未返回内容。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1147| RESET | 205 | 重置内容。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1148| PARTIAL | 206 | 部分内容。服务器成功处理了部分GET请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1149| MULT_CHOICE | 300 | 多种选择。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1150| MOVED_PERM | 301 | 永久移动。请求的资源已被永久的移动到新URI,返回信息会包括新的URI,浏览器会自动定向到新URI。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1151| MOVED_TEMP | 302 | 临时移动。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1152| SEE_OTHER | 303 | 查看其它地址。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1153| NOT_MODIFIED | 304 | 未修改。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1154| USE_PROXY | 305 | 使用代理。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1155| BAD_REQUEST | 400 | 客户端请求的语法错误,服务器无法理解。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1156| UNAUTHORIZED | 401 | 请求要求用户的身份认证。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1157| PAYMENT_REQUIRED | 402 | 保留,将来使用。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1158| FORBIDDEN | 403 | 服务器理解请求客户端的请求,但是拒绝执行此请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1159| NOT_FOUND | 404 | 服务器无法根据客户端的请求找到资源(网页)。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1160| BAD_METHOD | 405 | 客户端请求中的方法被禁止。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1161| NOT_ACCEPTABLE | 406 | 服务器无法根据客户端请求的内容特性完成请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1162| PROXY_AUTH | 407 | 请求要求代理的身份认证。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1163| CLIENT_TIMEOUT | 408 | 请求时间过长,超时。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1164| CONFLICT | 409 | 服务器完成客户端的PUT请求是可能返回此代码,服务器处理请求时发生了冲突。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1165| GONE | 410 | 客户端请求的资源已经不存在。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1166| LENGTH_REQUIRED | 411 | 服务器无法处理客户端发送的不带Content-Length的请求信息。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1167| PRECON_FAILED | 412 | 客户端请求信息的先决条件错误。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1168| ENTITY_TOO_LARGE | 413 | 由于请求的实体过大,服务器无法处理,因此拒绝请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1169| REQ_TOO_LONG | 414 | 请求的URI过长(URI通常为网址),服务器无法处理。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1170| UNSUPPORTED_TYPE | 415 | 服务器无法处理请求的格式。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1171| RANGE_NOT_SATISFIABLE<sup>12+</sup> | 416 | 请求范围不符合要求。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 1172| INTERNAL_ERROR | 500 | 服务器内部错误,无法完成请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1173| NOT_IMPLEMENTED | 501 | 服务器不支持请求的功能,无法完成请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1174| BAD_GATEWAY | 502 | 充当网关或代理的服务器,从远端服务器接收到了一个无效的请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1175| UNAVAILABLE | 503 | 由于超载或系统维护,服务器暂时的无法处理客户端的请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1176| GATEWAY_TIMEOUT | 504 | 充当网关或代理的服务器,未及时从远端服务器获取请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1177| VERSION | 505 | 服务器请求的HTTP协议的版本。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1178 1179## HttpResponse 1180 1181request方法回调函数的返回值类型。 1182 1183**系统能力**:SystemCapability.Communication.NetStack 1184 1185| 名称 | 类型 | 必填 | 说明 | 1186| -------------------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 1187| result | string \| Object \| ArrayBuffer | 是 | HTTP请求根据响应头中content-type类型返回对应的响应格式内容,若HttpRequestOptions无expectDataType字段,按如下规则返回:<br />- application/json:返回JSON格式的字符串;<br />- application/octet-stream:ArrayBuffer;<br />- image:ArrayBuffer;<br />- 其他:string。<br /> 若HttpRequestOption有expectDataType字段,开发者需传入与服务器返回类型相同的数据类型。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1188| resultType<sup>9+</sup> | [HttpDataType](#httpdatatype9) | 是 | 返回值类型。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1189| responseCode | [ResponseCode](#responsecode) \| number | 是 | 回调函数执行成功时,此字段为[ResponseCode](#responsecode)。若执行失败,错误码将会从AsyncCallback中的err字段返回。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1190| header | Object | 是 | 发起HTTP请求返回来的响应头。当前返回的是JSON格式字符串,如需具体字段内容,需开发者自行解析。常见字段及解析方式如下:<br/>- content-type:header['content-type'];<br />- status-line:header['status-line'];<br />- date:header.date/header['date'];<br />- server:header.server/header['server'];<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1191| cookies<sup>8+</sup> | string | 是 | 服务器返回的原始cookies。开发者可自行处理。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1192| performanceTiming<sup>11+</sup> | [PerformanceTiming](#performancetiming11) | 是 | HTTP请求的各个阶段的耗时。| 1193 1194## ClientCert<sup>11+</sup> 1195 1196客户端证书类型。 1197 1198**系统能力**:SystemCapability.Communication.NetStack 1199 1200| 名称 | 类型 | 必填 | 说明 | 1201| -------- | -------| --- | ----------- | 1202| certPath | string | 是 | 证书路径 | 1203| certType | [CertType](#certtype11) | 否 | 证书类型,默认是PEM | 1204| keyPath | string | 是 | 证书秘钥的路径 | 1205| keyPassword | string | 否 | 证书秘钥的密码 | 1206 1207## PerformanceTiming<sup>11+</sup> 1208 1209性能打点(单位:毫秒)。 1210 1211**系统能力**:SystemCapability.Communication.NetStack 1212 1213| 名称 | 类型 | 必填 | 说明 | 1214| ---------- | ------ | ---- | --------------------- | 1215| dnsTiming | number | 是 | 从[request](#request)请求到DNS解析完成耗时。 | 1216| tcpTiming | number | 是 | 从[request](#request)请求到TCP连接完成耗时。 | 1217| tlsTiming | number | 是 | 从[request](#request)请求到TLS连接完成耗时。 | 1218| firstSendTiming | number | 是 | 从[request](#request)请求到开始发送第一个字节的耗时。 | 1219| firstReceiveTiming | number | 是 | 从[request](#request)请求到接收第一个字节的耗时。 | 1220| totalFinishTiming | number | 是 | 从[request](#request)请求到完成请求的耗时。 | 1221| redirectTiming | number | 是 | 从[request](#request)请求到完成所有重定向步骤的耗时。 | 1222| responseHeaderTiming | number | 是 | 从[request](#request)请求到header解析完成的耗时。 | 1223| responseBodyTiming | number | 是 | 从[request](#request)请求到body解析完成的耗时。 | 1224| totalTiming | number | 是 | 从[request](#request)请求回调到应用程序的耗时。 | 1225 1226## DataReceiveProgressInfo<sup>11+</sup> 1227 1228数据接收信息 1229 1230**系统能力**:SystemCapability.Communication.NetStack 1231 1232| 名称 | 类型 | 必填 | 说明 | 1233| ---- | ---- | ---- | ---- | 1234| receiveSize | number | 是 | 已接收的数据量(字节)。 | 1235| totalSize| number | 是 | 总共要接收的数据量(字节)| 1236 1237## DataSendProgressInfo<sup>11+</sup> 1238 1239数据发送信息 1240 1241**系统能力**:SystemCapability.Communication.NetStack 1242 1243### 属性 1244 1245| 名称 | 类型 | 必填 | 说明 | 1246| ---- | ---- | ---- | ---- | 1247| sendSize | number | 是 | 每次发送的数据量(字节)。 | 1248| totalSize | number | 是 | 总共要发送的数据量(字节)。 | 1249 1250## MultiFormData<sup>11+</sup> 1251 1252多部分表单数据的类型。 1253 1254**系统能力**:SystemCapability.Communication.NetStack 1255 1256| 名称 | 类型 | 必填 | 说明 | 1257| ---- | ---- | ---- | ---- | 1258| name | string | 是 | 数据名称 | 1259| contentType | string | 是 | 数据类型,如'text/plain','image/png', 'image/jpeg', 'audio/mpeg', 'video/mp4'等 | 1260| remoteFileName | string | 否 | 上传到服务器保存为文件的名称。 | 1261| data | string \| Object \| ArrayBuffer | 否 | 表单数据内容。 | 1262| filePath | string | 否 | 此参数根据文件的内容设置mime部件的正文内容。用于代替data将文件数据设置为数据内容,如果data为空,则必须设置filePath。如果data有值,则filePath不会生效。| 1263 1264## http.createHttpResponseCache<sup>9+</sup> 1265 1266createHttpResponseCache(cacheSize?: number): HttpResponseCache 1267 1268创建一个HttpResponseCache对象,可用于存储HTTP请求的响应数据。对象中可调用flush与delete方法,cacheSize指定缓存大小。 1269 1270**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1271 1272**系统能力**:SystemCapability.Communication.NetStack 1273 1274**参数:** 1275 1276| 参数名 | 类型 | 必填 | 说明 | 1277| -------- | --------------------------------------- | ---- | ---------- | 1278| cacheSize | number | 否 | 缓存大小最大为10\*1024\*1024(10MB),默认最大。 | 1279 1280**返回值:** 1281 1282| 类型 | 说明 | 1283| :---------- | :----------------------------------------------------------- | 1284| [HttpResponseCache](#httpresponsecache9) | 返回一个存储HTTP访问请求响应的对象。 | 1285 1286**示例:** 1287 1288```ts 1289import { http } from '@kit.NetworkKit'; 1290 1291let httpResponseCache = http.createHttpResponseCache(); 1292``` 1293 1294## HttpResponseCache<sup>9+</sup> 1295 1296存储HTTP访问请求响应的对象。在调用HttpResponseCache的方法前,需要先通过[createHttpResponseCache()](#httpcreatehttpresponsecache9)创建一个任务。 1297 1298**响应头中的相应关键字使用** 1299 1300- **`Cache-Control`**:用于指定缓存策略,如`no-cache`, `no-store`, `max-age`, `public`, `private`等。 1301 1302- **`Expires`**:指定资源的过期时间,格式为GMT时间。 1303 1304- **`ETag`**:用于资源版本标识,客户端可以使用`If-None-Match`请求头来验证资源是否已更改。 1305 1306- **`Last-Modified`**:指定资源最后修改时间,客户端可以使用`If-Modified-Since`请求头来验证资源是否已更改。 1307 1308- **`Vary`**:指定哪些请求头的值会影响缓存的响应,用于区分不同的缓存版本。 1309 1310使用这些关键字时,服务器端需要正确配置响应头,客户端则需要根据这些响应头来决定是否使用缓存的资源,以及如何验证资源是否是最新的。正确的缓存策略可以显著提高应用的性能和用户体验。 1311 1312**如何设置Cache-Control头** 1313 1314`Cache-Control`为通用报头,但通常是在服务器端进行的,它允许你定义一个响应资源应该何时、如何被缓存以及缓存多长时间。以下是一些常用的`Cache-Control`指令及其含义: 1315 13161. **`no-cache`**:表示在使用缓存前,必须先去源服务器校验资源的有效性。如果资源未变更,则响应状态码为304(Not Modified),不发送资源内容,使用缓存中的资源。如果资源已经过期,则响应状态码为200,并发送资源内容。 1317 13182. **`no-store`**:表示不允许缓存资源,每次请求都必须从服务器获取资源。 1319 13203. **`max-age`**:指定缓存的最大时间(以秒为单位)。例如,`Cache-Control: max-age=3600`表示缓存的有效期为1小时。 1321 13224. **`public`**:表明响应可以被任何对象(包括:发送请求的客户端,代理服务器等)缓存。 1323 13245. **`private`**:表明响应只能被单个用户缓存,不能作为共享缓存(即代理服务器不能缓存它)。 1325 13266. **`must-revalidate`**:表示缓存必须在使用前验证旧资源的状态,并且在缓存过期后,必须重新验证资源。 1327 13287. **`no-transform`**:表示不允许代理服务器修改响应内容。 1329 13308. **`proxy-revalidate`**:与`must-revalidate`类似,但仅适用于共享缓存。 1331 13329. **`s-maxage`**:类似于`max-age`,但仅适用于共享缓存。 1333 1334### flush<sup>9+</sup> 1335 1336flush(callback: AsyncCallback\<void\>): void 1337 1338将缓存中的数据写入文件系统,以便在下一个HTTP请求中访问所有缓存数据,使用callback方式作为异步方法。缓存数据包括:响应头(header)、响应体(result)、cookies、请求时间(requestTime)和响应时间(responseTime)。 1339 1340**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1341 1342**系统能力**:SystemCapability.Communication.NetStack 1343 1344**参数:** 1345 1346| 参数名 | 类型 | 必填 | 说明 | 1347| -------- | --------------------------------------- | ---- | ---------- | 1348| callback | AsyncCallback\<void\> | 是 | 回调函数返回写入结果。 | 1349 1350**示例:** 1351 1352```ts 1353import { http } from '@kit.NetworkKit'; 1354import { BusinessError } from '@kit.BasicServicesKit'; 1355 1356let httpResponseCache = http.createHttpResponseCache(); 1357let httpRequest = http.createHttp(); 1358httpRequest.request("EXAMPLE_URL", (err: BusinessError, data: http.HttpResponse) => { 1359 if (!err) { 1360 httpResponseCache.flush((err: BusinessError) => { 1361 if (err) { 1362 console.error('flush fail'); 1363 } 1364 console.info('flush success'); 1365 }); 1366 httpRequest.destroy(); 1367 } else { 1368 console.error('error:' + JSON.stringify(err)); 1369 // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。 1370 httpRequest.destroy(); 1371 } 1372}); 1373``` 1374 1375### flush<sup>9+</sup> 1376 1377flush(): Promise\<void\> 1378 1379将缓存中的数据写入文件系统,以便在下一个HTTP请求中访问所有缓存数据,使用Promise方式作为异步方法。 1380 1381**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1382 1383**系统能力**:SystemCapability.Communication.NetStack 1384 1385**返回值:** 1386 1387| 类型 | 说明 | 1388| --------------------------------- | ------------------------------------- | 1389| Promise\<void\> | 以Promise形式返回写入结果。 | 1390 1391**示例:** 1392 1393```ts 1394import { http } from '@kit.NetworkKit'; 1395import { BusinessError } from '@kit.BasicServicesKit'; 1396 1397let httpRequest = http.createHttp(); 1398let httpResponseCache = http.createHttpResponseCache(); 1399let promise = httpRequest.request("EXAMPLE_URL"); 1400 1401promise.then((data: http.HttpResponse) => { 1402 httpResponseCache.flush().then(() => { 1403 console.error('flush success'); 1404 }).catch((err: BusinessError) => { 1405 console.info('flush fail'); 1406 }); 1407}).catch((err: Error) => { 1408 console.error('error:' + JSON.stringify(err)); 1409}); 1410``` 1411 1412### delete<sup>9+</sup> 1413 1414delete(callback: AsyncCallback\<void\>): void 1415 1416禁用缓存并删除其中的数据,使用callback方式作为异步方法。 1417 1418**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1419 1420**系统能力**:SystemCapability.Communication.NetStack 1421 1422**参数:** 1423 1424| 参数名 | 类型 | 必填 | 说明 | 1425| -------- | --------------------------------------- | ---- | ---------- | 1426| callback | AsyncCallback\<void\> | 是 | 回调函数返回删除结果。| 1427 1428**示例:** 1429 1430```ts 1431import { http } from '@kit.NetworkKit'; 1432import { BusinessError } from '@kit.BasicServicesKit'; 1433 1434let httpRequest = http.createHttp(); 1435httpRequest.request("EXAMPLE_URL").then(data => { 1436 const httpResponseCache = http.createHttpResponseCache(); 1437 httpResponseCache.delete((err: BusinessError) => { 1438 try { 1439 if (err) { 1440 console.error('fail: ' + err); 1441 } else { 1442 console.info('success'); 1443 } 1444 } catch (err) { 1445 console.error('error: ' + err); 1446 } 1447 }); 1448 httpRequest.destroy(); 1449}).catch((error: BusinessError) => { 1450 console.error("errocode" + JSON.stringify(error)); 1451}); 1452``` 1453 1454### delete<sup>9+</sup> 1455 1456delete(): Promise\<void\> 1457 1458禁用缓存并删除其中的数据,使用Promise方式作为异步方法。 1459 1460**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1461 1462**系统能力**:SystemCapability.Communication.NetStack 1463 1464**返回值:** 1465 1466| 类型 | 说明 | 1467| --------------------------------- | ------------------------------------- | 1468| Promise\<void\> | 以Promise形式返回删除结果。 | 1469 1470**示例:** 1471 1472```ts 1473import { http } from '@kit.NetworkKit'; 1474import { BusinessError } from '@kit.BasicServicesKit'; 1475 1476let httpRequest = http.createHttp(); 1477httpRequest.request("EXAMPLE_URL").then(data => { 1478 const httpResponseCache = http.createHttpResponseCache(); 1479 httpResponseCache.delete().then(() => { 1480 console.log("success"); 1481 }).catch((err: BusinessError) => { 1482 console.error("fail"); 1483 }); 1484 httpRequest.destroy(); 1485}).catch((error: BusinessError) => { 1486 console.error("errocode" + JSON.stringify(error)); 1487}); 1488``` 1489 1490## HttpDataType<sup>9+</sup> 1491 1492http的数据类型。 1493 1494**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1495 1496**系统能力**:SystemCapability.Communication.NetStack 1497 1498| 名称 | 值 | 说明 | 1499| ------------------ | -- | ----------- | 1500| STRING | 0 | 字符串类型。 | 1501| OBJECT | 1 | 对象类型。 | 1502| ARRAY_BUFFER | 2 | 二进制数组类型。| 1503 1504## HttpProtocol<sup>9+</sup> 1505 1506http协议版本。 1507 1508**系统能力**:SystemCapability.Communication.NetStack 1509 1510| 名称 | 值 | 说明 | 1511| :-------- | :----------- | :----------- | 1512| HTTP1_1 | 0 | 协议http1.1 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1513| HTTP2 | 1 | 协议http2 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 1514| HTTP3<sup>11+</sup> | 2 | 协议http3,若系统或服务器不支持,则使用低版本的http协议请求。<br />- 仅对https的URL生效,http则会请求失败。 | 1515 1516## CertType<sup>11+</sup> 1517 1518证书类型的枚举。 1519 1520**系统能力**:SystemCapability.Communication.NetStack 1521 1522| 名称 | 说明 | 1523| --- | ---------- | 1524| PEM | 证书类型PEM | 1525| DER | 证书类型DER | 1526| P12 | 证书类型P12 | 1527 1528## CertificatePinning<sup>12+</sup> 1529 1530由应用配置的证书。 1531 1532**系统能力**:SystemCapability.Communication.NetStack 1533 1534| 名称 | 类型 | 必填 |说明 | 1535| ------------------ |---- |-- | ----------- | 1536| publicKeyHash | string | 是 |字符串类型的证书PIN码。 | 1537| hashAlgorithm | 'SHA-256' | 是 |加密算法,当前仅支持该算法。 | 1538 1539## HttpProxy 1540 1541type HttpProxy = connection.HttpProxy 1542 1543网络代理配置信息。 1544 1545**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1546 1547**系统能力**:SystemCapability.Communication.NetStack 1548 1549| 类型 | 说明 | 1550| ---------------- | --------------------------- | 1551| connection.HttpProxy | 网络代理配置信息。 | 1552