惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
C
CERT Recently Published Vulnerability Notes
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Tenable Blog
O
OpenAI News
NISL@THU
NISL@THU
AI
AI
T
Threat Research - Cisco Blogs
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
W
WeLiveSecurity
P
Privacy International News Feed
Cisco Talos Blog
Cisco Talos Blog
S
Secure Thoughts
D
Docker
博客园 - 三生石上(FineUI控件)
T
Troy Hunt's Blog
T
Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
Vercel News
Vercel News
H
Hacker News: Front Page
B
Blog
S
SegmentFault 最新的问题
H
Heimdal Security Blog
T
The Blog of Author Tim Ferriss
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
AWS News Blog
AWS News Blog
N
News and Events Feed by Topic
L
LINUX DO - 热门话题
小众软件
小众软件
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
Hacker News: Ask HN
Hacker News: Ask HN
Recent Announcements
Recent Announcements
Recent Commits to openclaw:main
Recent Commits to openclaw:main
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
PCI Perspectives
PCI Perspectives
I
Intezer
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed

博客园 - wsky

生产环境使用Nuget keepalived安装和使用 NHibernate分享 PPT 开源选型关注点 SocketAsyncEventArgs stunnel+haproxy SSL以及问题记录 快速实现一个简单的bigpipe模型 Velocity2010大会回顾 小结一下关于bpm实施方面的交流 乱弹之企业应用 互联网企业流程部门价值方向的一些探讨 Live Writer的代码插件 再谈团队,项目,产品 【渐进】设计一个模拟并行的线程同步组件 【渐进】关于反向代理,负载均衡 【渐进】浅尝DDD,对"试卷"建模 【渐进】延迟加载机制的简易实现(上) - wsky - 博客园 略谈“开发效率” 分工,协作,团队
为WebClient增加Cookie支持
wsky · 2011-05-10 · via 博客园 - wsky

System.Net.WebClient是.net提供的高级api,使用上很便捷,但是默认的实现缺乏对cookie的支持,比如您希望使用它来进行模拟登录时,无法直接获取响应的cookie(它没有直接提供操作方法),google了一下,也发现了很简易的改进,如:

http://couldbedone.blogspot.com/2007/08/webclient-handling-cookies.html

http://codehelp.smartdev.eu/2009/05/08/improve-webclient-by-adding-useragent-and-cookies-to-your-requests/

就是重写GetWebRequest(Uri address)即可,不过实际场景这样可不够用,比如你要模拟登录的地址并非是要访问的目标地址时,比如登录过程有302重定向之类的,上述文章中的实现都会使得您携带的cookie并不是目标站点需要的,

那么我们来对它再做一下改进,既然重写了GetWebRequest,不如把GetWebResponse也重写一下吧:

   1:      /// <summary>
   2:      /// 支持cookie的webclient
   3:      /// <remarks>
   4:      /// 请求完成后会自动将响应cookie填充至CookieContainer
   5:      /// </remarks>
   6:      /// </summary>
   7:      internal class CookieAwareWebClient : WebClient
   8:      {
   9:          internal CookieContainer _cookieContainer = new CookieContainer();
  10:   
  11:          protected override WebRequest GetWebRequest(Uri address)
  12:          {
  13:              var request = base.GetWebRequest(address);
  14:              if (request is HttpWebRequest)
  15:                  (request as HttpWebRequest).CookieContainer = this._cookieContainer;
  16:              return request;
  17:          }
  18:          protected override WebResponse GetWebResponse(WebRequest request)
  19:          {
  20:              var r = base.GetWebResponse(request);
  21:              if (r is HttpWebResponse)
  22:                  this._cookieContainer.Add((r as HttpWebResponse).Cookies);
  23:              return r;
  24:          }
  25:      }

上述便是完整代码:)