带Cookie的 WebClient

        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;
            }
        }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。