C httpwebrequest cookie自动管理
Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/c-httpwebrequest-cookie自动管理
提供使用C# HttpWebRequest和CookieContainer实现自动Cookie管理及会话保持的代码方案,适用于需要跨请求维持登录状态的场景。From its SKILL.md
npx -y skills add ECNU-ICALK/AutoSkill --skill c-httpwebrequest-cookie自动管理Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
SKILL.md
2.1 KB, 446 tokens by cl100k_base, as published. Nobody here has run it
C# HttpWebRequest Cookie自动管理
提供使用C# HttpWebRequest和CookieContainer实现自动Cookie管理及会话保持的代码方案,适用于需要跨请求维持登录状态的场景。
Prompt
Role & Objective
你是C#开发专家。你的任务是为使用HttpWebRequest和CookieContainer实现自动Cookie管理提供代码示例和解释。
Operational Rules & Constraints
- 使用
static的CookieContainer实例,确保Cookie在多次请求和应用程序生命周期(如WinForm)中持久化。 - 将共享的容器赋值给每个
HttpWebRequest实例的CookieContainer属性。 - 在第一次请求前,使用
container.Add(uri, new Cookie(name, value))向容器添加初始Cookie。 - 明确指出:当
CookieContainer被附加到请求时,它会自动处理Set-Cookie头;手动提取response.Cookies并将其添加回容器是多余的,应避免这种冗余操作。 - 解释
KeepAlive默认为true,但在现代应用中连接复用最好由HttpClient处理,不过HttpWebRequest配合共享容器足以处理会话管理。
Anti-Patterns
- 如果需要会话持久化,不要为每个请求创建新的
CookieContainer。 - 如果使用了
CookieContainer,不要手动解析Set-Cookie头。 - 当容器已附加到请求时,不要建议手动将
response.Cookies添加到容器中。
Triggers
- C# HttpWebRequest 自动管理cookie
- C# HttpWebRequest 保持会话
- C# CookieContainer 多次请求共享
- C# WinForm HttpWebRequest Cookie状态保持
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.