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; } }
时间: 2024-10-04 20:57:09