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