zoukankan      html  css  js  c++  java
  • 带Cookie的 WebClient

        /// <summary>
        /// WebClient的扩展
        /// </summary>
        public class webClient : WebClient
        {
            /// <summary>是否重定向 默认为true</summary>
            public bool AllowAutoRedirect { get; set; }
            /// <summary>
            ///获取或设置 Referer HTTP 标头的值。默认值为 null。
            /// </summary>
            public string Referer { get; set; }
            /// <summary>
            /// 获取或设置 Content-type HTTP 标头的值。 默认: application/x-www-form-urlencoded
            /// </summary>
            public string ContentType { get; set; }
            /// <summary>
            /// 获取或设置 User-agent HTTP 标头的值。默认:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
            /// </summary>
            public string UserAgent { get; set; }
    
            /// <summary>
            /// 获取或设置 GetResponse 和 GetRequestStream 方法的超时值(以毫秒为单位)。 默认值是 30,000 毫秒(30 秒)。
            /// </summary>
            public int Timeout { get; set; }
            /// <summary>
            /// 获取或设置写入或读取流时的超时(以毫秒为单位)。默认值为 300,000 毫秒(5 分钟)
            /// </summary>
            public int ReadWriteTimeout { get; set; }
    
            /// <summary>获取或设置请求相关联的Cookie</summary>
            public CookieContainer CookieContainer { get; set; }
            /// <summary>创建一个新的 WebClient 实例。</summary>
            public webClient()
            {
                this.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)";
                this.ContentType = "application/x-www-form-urlencoded";
                this.CookieContainer = new CookieContainer();
                this.AllowAutoRedirect = true;
                this.Timeout = 30 * 1000;
                this.ReadWriteTimeout = 300 * 1000;
            }
            protected override WebRequest GetWebRequest(Uri address)
            {
                HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
                if (this.Referer != null) request.Referer = this.Referer;
                if (this.ContentType != null) request.ContentType = this.ContentType;
                if (this.UserAgent != null) request.UserAgent = this.UserAgent;
                request.AllowAutoRedirect = this.AllowAutoRedirect;
                request.CookieContainer = this.CookieContainer; //这句很关键,有了他可以保存返回的Cookie
                request.Timeout = this.Timeout;
                request.ReadWriteTimeout = this.ReadWriteTimeout;
                return request;
            }
        }
  • 相关阅读:
    springboot 入门
    json-lib 的 maven dependency 引入及Jar包下载
    DataGridView 使用技巧精华
    SqlServer无备份下误删数据恢复
    Reflector 已经out了,试试ILSpy
    C# 反射,动态编译
    windows8和windows server2012不联网安装.net 3.5(包括2.0和3.0)
    如何附加被分离的质疑数据库? [转]
    easyui validatebox 验证类型
    所见即所得:七大无需编程的DIY开发工具
  • 原文地址:https://www.cnblogs.com/chengulv/p/4436443.html
Copyright © 2011-2022 走看看