搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案