0

I use Flutter to make a HTTP-Post to a website to login. If I only send the data to login, the response of the HTTP-Post shows, that I`m not logged in. I tried to find out the cookie the Website sends. I did it with the software Postman. If I add the same cookie, I got at Postman, the HTTP-Post with Flutter works and the response shows, that Im logged in. After a while, the value of the cookie switched and the HTTP-Post via Flutter doent work.

How can I get the information about the actual value of the cookie from the website?

The code looks like this:

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");
} 
miwa
  • 49
  • 2
  • 7
  • Does this answer your question? [How do I can iterate cookie values from http response?](https://stackoverflow.com/questions/53667571/how-do-i-can-iterate-cookie-values-from-http-response) – Richard Heap Nov 17 '19 at 14:33

1 Answers1

0

This is how I get 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'];
}
hoangquyy
  • 1,893
  • 2
  • 14
  • 29