在 Flutter 中找出 Cookie

Find out Cookie in Flutter

我用Flutter做了一个HTTP-Post到一个网站登录。如果我只发送数据登录,HTTP-Post 的响应显示我没有登录。我试图找出网站发送的 cookie。我用软件 Postman 做到了。如果我添加相同的 cookie,我会在 Postman 处找到,带有 Flutter 的 HTTP-Post 工作并且响应显示我已登录。过了一会儿,cookie 的值切换了并且HTTP-Post 通过 Flutter 完成工作。

如何从网站上获取有关cookie实际值的信息?

代码如下所示:

Future initiate() async {
  ByteData bytes = await rootBundle.load('assets/Certificates/test.crt');
  SecurityContext clientContext = new SecurityContext()
    ..setTrustedCertificatesBytes(bytes.buffer.asUint8List());
  var client = new HttpClient(context: clientContext);

  IOClient ioClient = new IOClient(client);
  var form = <String, String>{
    'user':'testuser',
    'pass':'testpass',
    'logintype':'login',
    'pid':'128',
    'submit':'Anmelden',
    'tx_felogin_pi1[noredirect]':'0'
  };
  print(form);

  var ris = await ioClient.get("https://www.test.de/login");
  print(ris.headers);
  http.Response res = await ioClient.post("https://www.test.de/login",body:form, headers: {
    "Accept": "application/json",
    "Content-Type": "application/x-www-form-urlencoded", "Cookie": "fe_typo_user=12345"},  encoding: Encoding.getByName("utf-8"));
  ioClient.close();
  client.close();

  print(res.body.length);
  return res.body.contains("logout");
} 

这是我获取 cookie 的方式:

static Future<String> login(String username, String password) async {
  var url = _session._getUrl('/auth/login');
  var map = new Map<String, dynamic>();
  map['username'] = username;
  map['password'] = password;
  final response = await http.post(url, body: map);
  var jsonResponse = convert.jsonDecode(response.body);
  var cookies = response.headers['set-cookie'];
  if (cookies != null) {
    var csrf = _session._getCSRF(cookies);
    LocalStorage.instance.setString('CSRF', csrf);
    print(csrf);
  }
  return jsonResponse['msg'];
}