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

WebClient的超时问题及解决

时间:2014-07-16 17:00:01      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:blog   http   数据   art   io   问题   

WebClient的超时问题及解决  

转自:http://blog.163.com/xiaozhi797@126/blog/static/62440288201112245345838/

 

Webclient在下载请求时无法设置请求超时时间和请求读写超时时间。WebClient在异步下载时遇到网络不通等问题时没有响应超时造成app挂起。

1.Webclient请求超时设置
      重写Webclient的GetWebRequest方法,为HttpWebRequest添加请求超时及读写超时

      

bubuko.com,布布扣        protected override WebRequest GetWebRequest(Uri address)
bubuko.com,布布扣        {
bubuko.com,布布扣            HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
bubuko.com,布布扣            request.Timeout = 1000 * Timeout;
bubuko.com,布布扣            request.ReadWriteTimeout = 1000 * Timeout;
bubuko.com,布布扣            return request;
bubuko.com,布布扣        }

 

2.WebClient在异步下载

       创建计时器监视响应情况,过期则取消下载

      

bubuko.com,布布扣    public class Calculagraph
bubuko.com,布布扣    {
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 时间到事件
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        public event TimeoutCaller TimeOver;
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 开始时间
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        private DateTime _startTime;
bubuko.com,布布扣        private TimeSpan _timeout = new TimeSpan(0, 0, 10);
bubuko.com,布布扣        private bool _hasStarted = false;
bubuko.com,布布扣        object _userdata;
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 计时器构造方法
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="userdata">计时结束时回调的用户数据</param>
bubuko.com,布布扣        public Calculagraph(object userdata)
bubuko.com,布布扣        {
bubuko.com,布布扣            TimeOver += new TimeoutCaller(OnTimeOver);
bubuko.com,布布扣            _userdata = userdata;
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 超时退出
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="userdata"></param>
bubuko.com,布布扣        public virtual void OnTimeOver(object userdata)
bubuko.com,布布扣        {
bubuko.com,布布扣            Stop();
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 过期时间(秒)
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        public int Timeout
bubuko.com,布布扣        {
bubuko.com,布布扣            get
bubuko.com,布布扣            {
bubuko.com,布布扣                return _timeout.Seconds;
bubuko.com,布布扣            }
bubuko.com,布布扣            set
bubuko.com,布布扣            {
bubuko.com,布布扣                if (value <= 0)
bubuko.com,布布扣                    return;
bubuko.com,布布扣                _timeout = new TimeSpan(0, 0, value);
bubuko.com,布布扣            }
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 是否已经开始计时
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        public bool HasStarted
bubuko.com,布布扣        {
bubuko.com,布布扣            get
bubuko.com,布布扣            {
bubuko.com,布布扣                return _hasStarted;
bubuko.com,布布扣            }
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 开始计时
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        public void Start()
bubuko.com,布布扣        {
bubuko.com,布布扣            Reset();
bubuko.com,布布扣            _hasStarted = true;
bubuko.com,布布扣            Thread th = new Thread(WaitCall);
bubuko.com,布布扣            th.IsBackground = true;
bubuko.com,布布扣            th.Start();
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 重置
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        public void Reset()
bubuko.com,布布扣        {
bubuko.com,布布扣            _startTime = DateTime.Now;
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 停止计时
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        public void Stop()
bubuko.com,布布扣        {
bubuko.com,布布扣            _hasStarted = false;
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 检查是否过期
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <returns></returns>
bubuko.com,布布扣        private bool checkTimeout()
bubuko.com,布布扣        {
bubuko.com,布布扣            return (DateTime.Now - _startTime).Seconds >= Timeout;
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        private void WaitCall()
bubuko.com,布布扣        {
bubuko.com,布布扣            try
bubuko.com,布布扣            {
bubuko.com,布布扣                //循环检测是否过期
bubuko.com,布布扣                while (_hasStarted && !checkTimeout())
bubuko.com,布布扣                {
bubuko.com,布布扣                    Thread.Sleep(1000);
bubuko.com,布布扣                }
bubuko.com,布布扣                if (TimeOver != null)
bubuko.com,布布扣                    TimeOver(_userdata);
bubuko.com,布布扣            }
bubuko.com,布布扣            catch (Exception)
bubuko.com,布布扣            {
bubuko.com,布布扣                Stop();
bubuko.com,布布扣            }
bubuko.com,布布扣        }
bubuko.com,布布扣    }
bubuko.com,布布扣
bubuko.com,布布扣    /// <summary>
bubuko.com,布布扣    /// 过期时回调委托
bubuko.com,布布扣    /// </summary>
bubuko.com,布布扣    /// <param name="userdata"></param>
bubuko.com,布布扣    public delegate void TimeoutCaller(object userdata);

 

      

bubuko.com,布布扣    public class CNNWebClient : WebClient
bubuko.com,布布扣    {
bubuko.com,布布扣
bubuko.com,布布扣        private Calculagraph _timer;
bubuko.com,布布扣        private int _timeOut = 10;
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 过期时间
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        public int Timeout
bubuko.com,布布扣        {
bubuko.com,布布扣            get
bubuko.com,布布扣            {
bubuko.com,布布扣                return _timeOut;
bubuko.com,布布扣            }
bubuko.com,布布扣            set
bubuko.com,布布扣            {
bubuko.com,布布扣                if (value <= 0)
bubuko.com,布布扣                    _timeOut = 10;
bubuko.com,布布扣                _timeOut = value;
bubuko.com,布布扣            }
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 重写GetWebRequest,添加WebRequest对象超时时间
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="address"></param>
bubuko.com,布布扣        /// <returns></returns>
bubuko.com,布布扣        protected override WebRequest GetWebRequest(Uri address)
bubuko.com,布布扣        {
bubuko.com,布布扣            HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
bubuko.com,布布扣            request.Timeout = 1000 * Timeout;
bubuko.com,布布扣            request.ReadWriteTimeout = 1000 * Timeout;
bubuko.com,布布扣            return request;
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 带过期计时的下载
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        public void DownloadFileAsyncWithTimeout(Uri address, string fileName, object userToken)
bubuko.com,布布扣        {
bubuko.com,布布扣            if (_timer == null)
bubuko.com,布布扣            {
bubuko.com,布布扣                _timer = new Calculagraph(this);
bubuko.com,布布扣                _timer.Timeout = Timeout;
bubuko.com,布布扣                _timer.TimeOver += new TimeoutCaller(_timer_TimeOver);
bubuko.com,布布扣                this.DownloadProgressChanged += new DownloadProgressChangedEventHandler(CNNWebClient_DownloadProgressChanged);
bubuko.com,布布扣            }
bubuko.com,布布扣
bubuko.com,布布扣            DownloadFileAsync(address, fileName, userToken);
bubuko.com,布布扣            _timer.Start();
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// WebClient下载过程事件,接收到数据时引发
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="sender"></param>
bubuko.com,布布扣        /// <param name="e"></param>
bubuko.com,布布扣        void CNNWebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
bubuko.com,布布扣        {
bubuko.com,布布扣            _timer.Reset();//重置计时器
bubuko.com,布布扣        }
bubuko.com,布布扣
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// 计时器过期
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="userdata"></param>
bubuko.com,布布扣        void _timer_TimeOver(object userdata)
bubuko.com,布布扣        {
bubuko.com,布布扣            this.CancelAsync();//取消下载
bubuko.com,布布扣        }
bubuko.com,布布扣    }

WebClient的超时问题及解决,布布扣,bubuko.com

WebClient的超时问题及解决

标签:blog   http   数据   art   io   问题   

原文地址:http://www.cnblogs.com/elim/p/3848472.html

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