GithubHelp home page GithubHelp logo

shuihuadx / cookie_jar Goto Github PK

View Code? Open in Web Editor NEW

This project forked from flutterchina/cookie_jar

0.0 1.0 0.0 69 KB

A cookie manager for http requests in Dart, by which you can deal with the complex cookie policy and persist cookies easily.

License: MIT License

Dart 100.00%

cookie_jar's Introduction

CookieJar

Pub support

A cookie manager for http requests in Dart, by which you can deal with the complex cookie policy and persist cookies easily.

Add dependency

dependencies:
  cookie_jar: 3.0.1

Usage

A simple usage example:

import 'package:cookie_jar/cookie_jar.dart';
void main() async {
  List<Cookie> cookies = [Cookie("name", "wendux"),Cookie("location", "china")];
  var cj = CookieJar();
  //Save cookies   
  await cj.saveFromResponse(Uri.parse("https://www.baidu.com/"), cookies);
  //Get cookies  
  List<Cookie> results = await cj.loadForRequest(Uri.parse("https://www.baidu.com/xx"));
  print(results);  
}    
       

Classes

SerializableCookie

This class is a wrapper for Cookie class. Because the Cookie class doesn't support Json serialization, for the sake of persistence, we use this class instead of it.

CookieJar

CookieJar is a default cookie manager which implements the standard cookie policy declared in RFC. CookieJar saves the cookies in RAM, so if the application exit, all cookies will be cleared. A example as follow:

var cj= CookieJar();

PersistCookieJar

PersistCookieJar is a cookie manager which implements the standard cookie policy declared in RFC. PersistCookieJar persists the cookies in files, so if the application exit, the cookies always exist unless call delete explicitly. A example as follows:

// Cookie files will be saved in files in "./cookies"
var cj = PersistCookieJar(
    ignoreExpires:true, //save/load even cookies that have expired.
);

Note: In Flutter, File system is different from PC, you can use path_provider package to get the path :

// API `getTemporaryDirectory` is from "path_provider" package.
Directory tempDir = await getTemporaryDirectory();
var tempPath = tempDir.path;
var cj = PersistCookieJar(
          ignoreExpires: true,
          storage: FileStorage(tempPath)
        );

Storage

Now, You can customize your own storage,for more details refer to the implementation of FileStorage

APIs

Future saveFromResponse(Uri uri, List cookies);

Save the cookies for specified uri.

Future<List> loadForRequest(Uri uri);

Load the cookies for specified uri.

Future delete(Uri uri,[bool withDomainSharedCookie = false] )

Delete cookies for specified uri. This API will delete all cookies for the uri.host, it will ignored the uri.path.

If withDomainSharedCookie is true , will delete the domain-shared cookies.

Future deleteAll();

Delete all cookies 。

Working with HttpClient

Using CookieJar or PersistCookieJar manages HttpClient 's request/response cookies is very easy:

var cj=CookieJar();
...
request = await httpClient.openUrl(options.method, uri);
request.cookies.addAll(await cj.loadForRequest(uri));
response = await request.close();
await cj.saveFromResponse(uri, response.cookies);

Working with dio

dio is a powerful Http client for Dart, which supports Interceptors, Global configuration, FormData, File downloading, Timeout etc. And dio supports to manage cookies with cookie_jar, the simple example is:

import 'package:dio/dio.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:cookie_jar/cookie_jar.dart';

main() async {
  var dio = Dio();
  var cookieJar=CookieJar();
  dio.interceptors.add(CookieManager(cookieJar));
  await dio.get("https://baidu.com/");
  // Print cookies
  print(await cookieJar.loadForRequest(Uri.parse("https://baidu.com/")));
  // second request with the cookie
  await dio.get("https://baidu.com/");
}

Note: cookieJar 3.0 need:

  • dio version >= 4.0
  • dio_cookie_manager >= 3.0

More details about dio see : https://github.com/flutterchina/dio .

Copyright & License

This open source project authorized by https://flutterchina.club , and the license is MIT.

Features and bugs

Please file feature requests and bugs at the issue tracker.

cookie_jar's People

Contributors

wendux avatar brammittendorff avatar long1eu avatar littlemango avatar redsolver avatar shuihuadx avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.