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

推荐订阅源

The Last Watchdog
The Last Watchdog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 热门话题
G
GRAHAM CLULEY
S
Schneier on Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
Recorded Future
Recorded Future
I
Intezer
云风的 BLOG
云风的 BLOG
博客园 - Franky
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
T
Tenable Blog
The Hacker News
The Hacker News
T
The Blog of Author Tim Ferriss
Attack and Defense Labs
Attack and Defense Labs
D
DataBreaches.Net
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
News and Events Feed by Topic
有赞技术团队
有赞技术团队
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
N
News and Events Feed by Topic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
The Cloudflare Blog
Webroot Blog
Webroot Blog
W
WeLiveSecurity
H
Heimdal Security Blog
博客园 - 三生石上(FineUI控件)
V
Vulnerabilities – Threatpost
G
Google Developers Blog
O
OpenAI News
V
V2EX
罗磊的独立博客
博客园_首页
N
News | PayPal Newsroom
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
TaoSecurity Blog
TaoSecurity Blog
Cloudbric
Cloudbric
H
Hacker News: Front Page
博客园 - 叶小钗
T
Tor Project blog
AI
AI

博客园 - 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:      }

上述便是完整代码:)