目录

HTTP请求配置常见问题

Q:HTTP接口如何设置Cookie?

A:HttpRequestOptions中的header是一个Object类型,可以直接在header里设置cookie。调用httpRequest.request()需要申请权限:ohos.permission.INTERNET。使用时,httpRequest.request()接口中的“EXAMPLE_URL”需要替换成实际请求地址。参考代码如下:

import { http } from '@kit.NetworkKit';

@Entry
@Component
struct HttpRequest {
  @State message: string = '发起请求';

  request() {
    let httpRequest = http.createHttp();
    let options: http.HttpRequestOptions = {
      method: http.RequestMethod.POST,
      extraData: 'data to send',
      expectDataType: http.HttpDataType.STRING,
      priority: 1,
      header: {
        'cookie': 'key1=value1;key2=value2'
      }
    };
    httpRequest.request("EXAMPLE_URL", options, (err: Error, data: http.HttpResponse) => {
      if (!err) {
        console.info('Result:' + data.result);
        console.info('code:' + data.responseCode);
        console.info('type:' + JSON.stringify(data.resultType));
        console.info('header:' + JSON.stringify(data.header));
        console.info('cookies:' + data.cookies); // Starting from API version 8, cookies are supported
      } else {
        console.info('error:' + JSON.stringify(err));
      }
    });
  }

  build() {
    Row() {
      Column() {
        Button(this.message)
          .fontSize(30)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            this.request();
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

参考链接:@ohos.net.http (数据请求)。

Q:http网络请求中extraData支持的数据格式有哪些?

A:extraData代表发送请求的额外数据,支持如下数据:

Q:http请求的官方示例代码中的extraData是什么类型?

A:关于extraData的类型说明如下:

  1. 文档中对extraData的定义是“extraData?: string | Object | ArrayBuffer”,也就是extraData支持string、Object和ArrayBuffer三种类型。
  2. 有如下三种方法可供选择:
    1)extraData:"data to send";
    2)extraData:{ data: "data to send", };
    3)extraData:{ data: new ArrayBuffer(1)};

参考链接:HttpRequestOptions。

Q:能否通过httpResponse的result拿到一个加密内容的数据?

A:使用HTTP请求传递加密数据,可以在result中获取到结果。

HTTP请求根据响应头中content-type类型返回对应的响应格式内容,若HttpRequestOptions无expectDataType字段,按如下规则返回:

若HttpRequestOption有expectDataType字段,开发者需传入与服务器返回类型相同的数据类型。

参考链接:request。