码迷,mamicode.com
首页 > Web开发 > 详细

带Cookie的 WebClient

时间:2015-04-18 01:13:20      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

        public class webClient : WebClient
        {
            /// <summary>是否重定向 默认为true</summary>
            public bool AllowAutoRedirect { get; set; }

            /// <summary>获取或设置请求相关联的Cookie</summary>
            public CookieContainer CookieContainer { get; set; }
            /// <summary>创建一个新的 WebClient 实例。</summary>
            public webClient()
            {
                this.CookieContainer = new CookieContainer();
                this.AllowAutoRedirect = true;
            }
            protected override WebRequest GetWebRequest(Uri address)
            {
                HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
                request.AllowAutoRedirect = this.AllowAutoRedirect;
                request.CookieContainer = this.CookieContainer; //这句很关键,有了他可以保存返回的Cookie
                return request;
            }
        }

 

带Cookie的 WebClient

标签:

原文地址:http://www.cnblogs.com/chengulv/p/4436443.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!