如何从http响应中迭代cookie值? [英] How do I can iterate cookie values from http response?

查看:36
本文介绍了如何从http响应中迭代cookie值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在flutter应用程序中使用rest API.对于进一步的请求,我需要从个人资料API收到的JSESSIONID.我成功获得了回应,但我需要迭代cookie值的指南.

I am using rest API in my flutter app. For further request I need JSESSIONID which I received from my profile API. I successful got response but I need guide to iterate cookie value.

我遵循以下步骤:

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

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

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

As print raw cookie it is printing details:

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

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

I can iterate it by substring but I want to iterate it with a proper way. So please guide me on this.

推荐答案

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

With package:http you need to split the cookie string yourself using String.split. If you want to use the underlying http client, that gives you a pre-parsed list of cookies, for example:

  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>

这篇关于如何从http响应中迭代cookie值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆