Creating a Cookie Aware WebClient
来源:互联网 发布:iphone电池保养 知乎 编辑:程序博客网 时间:2024/06/15 15:54
C# has an object called WebClient that makes it easier to execute POSTs and GETs. But is doesn't handle the cookie.
the follow class handle the cookies:
public class CookieAwareWebClient : WebClient{ public CookieContainer CookieContainer { get; set; } public Uri Uri { get; set; } public CookieAwareWebClient() : this (new CookieContainer()) { } public CookieAwareWebClient(CookieContainer cookies) { this.CookieContainer = cookies; } public override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); if (request is HttpWebRequest){ (request as HttpWebRequest).CookieContainer = this.CookieContainer;}HttpWebRequest httpRequest = (HttpWebRequest) request;httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;return httpRequest; } public override WebResponse GetWebResponse(WebRequest request) { WebResponse response = base.GetWebResponse(request); //this string contains the cookies dataString setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];if (setCookieHeader != null){ ////do something if needed to parse out the cookie.// NOTE: the cookies from the request is handled by the response itself, don't need add again.// if you want to create your cookie, do the following. //if (setCookieHeader != null) //{ Cookie cookie = new Cookie(); //create cookiethis.CookieContainer.Add(cookie); //}}return response; }}
You will see two overridden methods for GetWebRequest and GetWebResponse. These methods can be overridden to handle the cookie container. You will also notice the AutomaticDecompression that can be placed on the HttpWebRequest to validate the response is being unzipped (GZIP) automatically since the WebClient doesn't do this out of the box.
Now, when calling web services or executing POSTs or GETs, it will handle holding your cookies for you automatically. They are included in subsequent requests
How to use:
1. use cookie. CookieContainer cookies = new CookieContainer ();WebClient wc = new CookieAwareWebClient(cookies);wc.Encoding = Encoding.UTF8; // send request, the class will get the cookies from the response and store the cookies in its CookieContainer (property);string data = wc.UploadString(uri, postStr); // or = wc.DownloadString(uri);// get the cookies from classcookies = ((CookieAwareWebClient)wc).CookieContainer; // now you can create new query with the cookies.WebClient wc = new CookieAwareWebClient(cookies); (same as above);
- Creating a Cookie Aware WebClient
- WebClient-Session和Cookie
- WebClient-Session和Cookie
- WebClient的cookie
- WebClient添加Cookie
- WebClient 保持 Session 和 Cookie
- WebClient 保持 Session 和 Cookie
- WebClient Post 数据 ,支持Cookie
- SQL Programming(6):Creating Data-Aware Forms
- Aware
- Creating a Scrollable DataGrid...
- Creating a Transparent Form
- Creating a Multithreaded Service
- Creating a Delete Button
- Creating a clickable map
- Creating a WCF ACD
- Creating a New Project
- Creating a Parameter Block
- Sql函数返回一个数据集(Table)
- redirect、redirect-action、chain三者的区别!
- 网民《潜伏》总结的所谓职场攻略
- JCR简介
- websphere application server v8.5 liberty profile试用
- Creating a Cookie Aware WebClient
- Plan Tools see myself
- row lock waits的两种情况
- struts2中常用Result类型的用法
- android 反编译apk文件
- 简单的单点登录令牌颁发转发制作,利用POST与置换Form完成
- QT延时
- [re: yuan] 简简单单的记录访问 url 的代理服务器
- Android学习笔记之ImageView