如何从http响应中迭代cookie值?

时间:2018-12-07 10:23:08

标签: dart flutter

我在Flutter应用中使用rest API。对于进一步的请求,我需要从个人资料API收到的JSESSIONID。我成功获得了答复,但需要指导来迭代cookie值。

我遵循以下步骤:

final response = await http.get(
      strURL,
      headers: {
        "Authorization": basicAuth,
        "Content-Type": "application/json"
      },
    );

    String rawCookie = response.headers['set-cookie'];
    print('rawCookie $rawCookie');

作为打印原始cookie,它正在打印详细信息:

flutter: rawCookie __cfduid=d5bbe3f8a131478a78ae996e636cca0401544177738; expires=Sat, 07-Dec-19 10:15:38 GMT; path=/; domain=.rayz.ch; HttpOnly,JSESSIONID=6AD6698C5BFC90F1D089696A955E6824; Path=/; HttpOnly

我可以通过子字符串对其进行迭代,但是我想以适当的方式对其进行迭代。所以请指导我。

1 个答案:

答案 0 :(得分:2)

对于package:http,您需要使用String.split自己分割cookie字符串。如果您想使用基础的http客户端,那么将为您提供一个预先解析的cookie列表,例如:

  HttpClient _httpClient = new HttpClient();
  HttpClientRequest request = await _httpClient.postUrl(Uri.parse(url));
  request.headers.set('content-type', 'application/json');
  request.add(utf8.encode(json.encode(jsonMap)));
  HttpClientResponse response = await request.close();
  print(response.cookies); // this is a List<Cookie>