搜索插件、代码仓与 APP 分类

插件详情v4.0.9

cookie_jar

一个用于 Dart 中 HTTP 请求的 Cookie 管理器,帮助您处理 Cookie 策略和持久化。

AndroidiOSmacOSWindowsLinux
近 30 天下载
24.7万
喜欢数
105
Pub 分数
160

安装插件

flutter pub add cookie_jar

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Dart 包,用于管理 HTTP 请求的 Cookie,实现对 Cookie 策略的无缝处理以及跨会话的持久化存储。它与 HTTP 客户端集成,可根据域名和路径规则自动存储、检索和发送 Cookie。适用于需要与 Web 服务进行有状态交互的应用程序,例如登录会话或用户偏好设置。该包支持 Android、iOS、macOS、Windows 和 Linux 多个平台,确保 Cookie 管理在跨平台环境中的一致性。

适合场景

  • 管理Web API中的会话Cookie
  • 保持用户登录状态
  • 自动化 HTTP 请求中的 Cookie 处理
  • 支持有状态的Web交互
  • 启用安全的 cookie 存储

主要特点

  • 自动 Cookie 持久化
  • 跨平台兼容性
  • 与 HTTP 客户端集成
  • 基于策略的Cookie管理
  • 安全存储机制

适合对象

  • 需要会话管理的 Web API 客户端
  • 需要长期存储 Cookie 的应用程序
  • 开发经过身份验证的Web服务的开发者
  • 跨平台移动和桌面应用
  • 具有复杂 Cookie 策略的应用程序

使用建议

  • 需要与 HTTP 客户端实例进行手动集成
  • 可能需要为自定义存储进行额外配置
  • 在没有额外防护措施的情况下,不适用于高安全环境
  • 仅限基本的 Cookie 操作
  • 取决于底层HTTP客户端实现

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

CookieJar

Pub GitHub license

A cookie manager for http requests in Dart, help you to deal with the cookie policies and persistence.

Get started

Checkout the Migration Guide for breaking changes between versions.

Add dependency

You can use the command to add dio as a dependency with the latest stable version:

$ dart pub add cookie_jar

Or you can manually add cookie_jar into the dependencies section in your pubspec.yaml:

dependencies:
  cookie_jar: ^replace-with-latest-version

The latest version is: Pub The latest version including pre-releases is: Pub

Usage

A simple usage example:

import 'package:cookie_jar/cookie_jar.dart';

void main() async {
  final cookieJar = CookieJar();
  List<Cookie> cookies = [Cookie('name', 'wendux'), Cookie('location', 'china')];
  // Saving cookies.
  await cookieJar.saveFromResponse(Uri.parse('https://pub.dev/'), cookies);
  // Obtain cookies.
  List<Cookie> results = await cookieJar.loadForRequest(Uri.parse('https://pub.dev/paths'));
  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 cookie container and manager for HTTP requests.

DefaultCookieJar

DefaultCookieJar is a default cookie manager which implements the standard cookie policy declared in RFC. It saves the cookies in the memory, all cookies will be cleared after the app exited.

To use it:


final cookieJar = 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.

To use it:

// Cookie files will be saved in files in "./cookies/4/"
final cookieJar = PersistCookieJar(
  ignoreExpires: true, // Save/load even cookies that have expired.
);

Note: When using the [FileStorage] in Flutter apps, use path_provider to obtain available directories.


Directory tempDir = await

getTemporaryDirectory();

final tempPath = tempDir.path;
final cookieJar = PersistCookieJar(
  ignoreExpires: true,
  storage: FileStorage(tempPath),
);
Storage

You can customize your own storage by extending Storage, see FileStorage for more details.

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 ignore the uri.path.

If withDomainSharedCookie is true , all domain-shared cookies will be deleted.

Future deleteAll();

Delete all cookies.

Working with HttpClient

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

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

Working with dio

dio is a powerful HTTP client for Dart/Flutter, which supports global configuration, interceptors, FormData, request cancellation, file uploading/downloading, timeout, and custom adapters etc. dio also supports to manage cookies with cookie_jar using dio_cookie_manager. For example:

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

void main() async {
  final dio = Dio();
  final cookieJar = CookieJar();
  dio.interceptors.add(CookieManager(cookieJar));
  await dio.get('https://pub.dev/');
  // Print cookies
  print(await cookieJar.loadForRequest(Uri.parse('https://pub.dev/')));
  // Another request with the cookie.
  await dio.get("https://pub.dev/");
}

More details about dio.

Copyright & License

The project and it's underlying projects are originally authored by @wendux with the organization @flutterchina since 2018.

The project consents the MIT license.

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目