#http-request #cookie-jar #cookies #store #http

reqwest_cookie_store

一个简单的crate,提供对reqwest::cookie::CookieStore trait的cookie_store::CookieStore实现的简单实现。

13个版本 (7个破坏性)

0.8.0 2024年5月31日
0.7.0 2024年3月23日
0.6.0 2023年6月17日
0.5.0 2022年11月5日
0.1.5 2021年5月12日

#19HTTP客户端

Download history 2360/week @ 2024-04-30 1948/week @ 2024-05-07 2063/week @ 2024-05-14 1798/week @ 2024-05-21 2456/week @ 2024-05-28 2658/week @ 2024-06-04 2402/week @ 2024-06-11 2386/week @ 2024-06-18 2749/week @ 2024-06-25 2527/week @ 2024-07-02 2511/week @ 2024-07-09 2990/week @ 2024-07-16 3711/week @ 2024-07-23 3692/week @ 2024-07-30 4233/week @ 2024-08-06 5851/week @ 2024-08-13

18,023 每月下载量
30 个crate中 使用(24个直接使用)

MIT/Apache

15KB
147 行代码

Documentation

reqwest_cookie_storecookie_store 提供了 reqwest::cookie::CookieStore 的实现。

示例

以下示例演示了从磁盘加载一个cookie_store::CookieStore(在此crate中重导出),并在一个CookieStoreMutex中使用它。然后它进行一系列请求,在之间检查和修改底层cookie_store::CookieStore的内容。

// Load an existing set of cookies, serialized as json
let cookie_store = {
  let file = std::fs::File::open("cookies.json")
      .map(std::io::BufReader::new)
      .unwrap();
  // use re-exported version of `CookieStore` for crate compatibility
  reqwest_cookie_store::CookieStore::load_json(file).unwrap()
};
let cookie_store = reqwest_cookie_store::CookieStoreMutex::new(cookie_store);
let cookie_store = std::sync::Arc::new(cookie_store);
{
  // Examine initial contents
  println!("initial load");
  let store = cookie_store.lock().unwrap();
  for c in store.iter_any() {
    println!("{:?}", c);
  }
}

// Build a `reqwest` Client, providing the deserialized store
let client = reqwest::Client::builder()
    .cookie_provider(std::sync::Arc::clone(&cookie_store))
    .build()
    .unwrap();

// Make a sample request
client.get("https://google.com").send().await.unwrap();
{
  // Examine the contents of the store.
  println!("after google.com GET");
  let store = cookie_store.lock().unwrap();
  for c in store.iter_any() {
    println!("{:?}", c);
  }
}

// Make another request from another domain
println!("GET from msn");
client.get("https://msn.com").send().await.unwrap();
{
  // Examine the contents of the store.
  println!("after msn.com GET");
  let mut store = cookie_store.lock().unwrap();
  for c in store.iter_any() {
    println!("{:?}", c);
  }
  // Clear the store, and examine again
  store.clear();
  println!("after clear");
  for c in store.iter_any() {
    println!("{:?}", c);
  }
}

// Get some new cookies
client.get("https://google.com").send().await.unwrap();
{
  // Write store back to disk
  let mut writer = std::fs::File::create("cookies2.json")
      .map(std::io::BufWriter::new)
      .unwrap();
  let store = cookie_store.lock().unwrap();
  store.save_json(&mut writer).unwrap();
}

依赖关系

~5–16MB
~225K SLoC