在Flutter中查找Cookie

问题描述 投票:0回答:1

我使用Flutter将HTTP发布到网站以进行登录。如果仅发送数据进行登录,则HTTP-Post的响应将显示我尚未登录。我试图找出网站发送的Cookie。我用软件Postman完成的。如果添加相同的cookie,我在邮递员那里,使用Flutter的HTTP-Post正常工作,并且响应显示Im已登录。过了一会儿,cookie的值切换了,并且通过Flutter的HTTP-Post起作用了。

如何从网站上获取有关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");
} 
authentication cookies flutter http-post httpcookie
1个回答
0
投票

这是我获取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'];
}
© www.soinside.com 2019 - 2024. All rights reserved.