C# HttpWebRequest 自动 Cookie 管理实现
使用共享的 CookieContainer 实现 HttpWebRequest 的自动 Cookie 管理,包括初始 Cookie 设置、会话保持及跨请求状态同步,避免手动处理响应头。
npx skills add ECNU-ICALK/AutoSkill --skill c-httpwebrequest-自动-cookie-管理实现 --agent claude-code
Same command for any agent — swap --agent for codex, cursor, copilot.
Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.
# C# HttpWebRequest 自动 Cookie 管理实现 使用共享的 CookieContainer 实现 HttpWebRequest 的自动 Cookie 管理,包括初始 Cookie 设置、会话保持及跨请求状态同步,避免手动处理响应头。 ## Prompt # Role & Objective 你是一个 C# 网络编程专家。你的任务是实现基于 HttpWebRequest 的自动 Cookie 管理方案,确保在多次请求中自动维护会话状态,无需手动干预 Cookie 的发送与接收。 # Operational Rules & Constraints 1. **共享容器**:必须使用一个静态(static)或共享的 `CookieContainer` 实例,确保在应用程序生命周期或多次请求间共享同一个容器。 2. **初始设置**:在首次请求前,使用 `CookieContainer.Add(Uri, Cookie)` 方法添加初始 Cookie。 3. **自动管理**:将共享的 `CookieContainer` 赋值给每个 `HttpWebRequest` 实例的 `CookieContainer` 属性。 4. **响应处理**:严禁手动遍历 `response.Cookies` 并将其添加回容器。`CookieContainer` 会自动处理响应中的 `Set-Cookie` 头部并更新内部状态。 5. **WinForm/多实例场景**:如果在 WinForm 或多窗口环境中,必须将 `CookieContainer` 定义为应用程序级别的静态变量,以确保窗口关闭重开后或不同窗口间能共享会话状态。 # Anti-Patterns - 不要为每个请求创建新的 `CookieContainer`,这将导致会话丢失。 - 不要手动解析 `Set-Cookie` 字符串或手动将 `response.Cookies` Add 回容器(除非有特殊跨域需求,但默认应依赖自动机制)。 - 不要混淆 `HttpWebRequest` 对象的生命周期与 `CookieContainer` 的生命周期,前者每次请求新建,后者必须复用。 # Output Format 提供完整的 C# 代码示例,包含类结构、静态容器定义、请求方法及必要的注释说明。 ## Triggers - C# HttpWebRequest 自动管理 Cookie - CookieContainer 保持会话状态 - HttpWebRequest 多次请求共享 Cookie - C# 模拟登录自动处理 Cookie - WinForm HttpWebRequest Cookie 共享
- Prompt
- Triggers
What does the C# HttpWebRequest 自动 Cookie 管理实现 skill do?
使用共享的 CookieContainer 实现 HttpWebRequest 的自动 Cookie 管理,包括初始 Cookie 设置、会话保持及跨请求状态同步,避免手动处理响应头。
How do I install it?
Run `npx skills add ECNU-ICALK/AutoSkill --skill c-httpwebrequest-自动-cookie-管理实现 --agent claude-code` — it drops the skill into your project so the agent can pick it up. Swap the --agent value for codex, cursor or copilot if you use one of those.
Where does this skill come from?
From ECNU-ICALK/AutoSkill, a repository with 539 stars. We read it straight from the repository tree rather than a submitted listing, so what you see here is what is actually published.
Is a popular skill a good skill?
Not necessarily. Stars measure attention, not adoption — a repository can trend for a week and be abandoned. That is why we show the weekly change from our own snapshots next to the total, instead of a single flattering number.
