Cookies for Session ID not being saved from Dio and CookieJar
Asked Answered
D

1

7

I am trying to make a web-scraper API for a specific website, which has some resources locked behind a login, and the login gives a session ID in the Cookies. When I tried POSTing my login, the session ID wasn't saved to CookieJar.

Code:

var dio = new Dio()
..interceptors.add(CookieManager(CookieJar()));

login(String username, String password) async{
    
    (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) { client.badCertificateCallback = (X509Certificate cert, String host, int port) => true; return client; };
    var response1 =await dio.post("https://www.codestepbystep.com/login", data:{"usernameoremail": username, "password":password, "login":"Login", "userkeepmeloggedin":"on", "after" : "after", "timezone" : "America/Chicago", "timezoneoffset" : "-360"});
    debugPrint(response1.headers.toString());
    var response = await dio.get("https://www.codestepbystep.com/problem/view/c/basics/hello_world");
    debugPrint(response.headers.toString());
  }

POST Headers :

I/flutter (11736): set-cookie: JSESSIONID=60C35F7306479DCC3A6E33C675D05995; Path=/; Secure; HttpOnly
I/flutter (11736): cache-control: no-cache
I/flutter (11736): transfer-encoding: chunked
I/flutter (11736): date: Wed, 04 Sep 2019 22:25:26 GMT
I/flutter (11736): content-type: text/html;charset=ISO-8859-1
I/flutter (11736): server: Apache-Coyote/1.1
I/flutter (11736): expires: Thu, 01 Dec 1994 16:00:00 GMT

GET Headers :

I/flutter (11736): content-type: text/html;charset=ISO-8859-1
I/flutter (11736): cache-control: no-cache
I/flutter (11736): date: Wed, 04 Sep 2019 22:25:26 GMT
I/flutter (11736): transfer-encoding: chunked
I/flutter (11736): server: Apache-Coyote/1.1
I/flutter (11736): expires: Thu, 01 Dec 1994 16:00:00 GMT

For whatever reason, the cookies aren't present in the GET Headers as the cookies are not saved. I tried copying the headers from POST and using them in my GET request, but I still couldn't access the specific resource on the website from GET. How can I get the cookies to save properly?

Deibel answered 5/9, 2019 at 0:59 Comment(0)
R
0

You should use the PersistCookieJar instead. Import path_provider and dio_cookie_manager packages into your app and use as follows:

     import 'package:path_provider/path_provider.dart';
     import 'package:dio_cookie_manager/dio_cookie_manager.dart';

     Directory appDocDir = await getApplicationDocumentsDirectory();
        String appDocPath = appDocDir.path;
    
        var cookieJar = PersistCookieJar(
            ignoreExpires: true, storage: FileStorage(appDocPath + "/.cookies/"));
    
        Constants.dio.interceptors.add(CookieManager(cookieJar));
Rinee answered 27/1, 2022 at 1:39 Comment(1)
doesn't work stillHollyanne

© 2022 - 2024 — McMap. All rights reserved.