示例#1
0
        public bool LoadToFile(string file, bool exportRequest = false)
        {
            bool ret = false;
            //FileStream fs = null;

            if (__trace)
                pb.Trace.WriteLine("Http.LoadToFile()");

            zfile.CreateFileDirectory(file);
            //fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read);

            Action action = () =>
            {
                StreamTransfer streamTransfer = new StreamTransfer();
                streamTransfer.SourceLength = _resultContentLength;
                ///////////////////streamTransfer.Progress.ProgressChanged += new Progress.ProgressChangedEventHandler(StreamTransferProgressChange);
                using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read))
                    ret = streamTransfer.Transfer(GetResponseStream(), fs);
                SetRequestDuration(DateTime.Now - RequestTime);
                //if (exportRequest)
                //    ExportRequest(file);
            };

            Action retry = () =>
            {
                //FileStream fs2 = fs;
                //fs = null;
                //fs2.Close();
                //fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read);
            };

            Action final = () =>
            {
                //if (fs != null)
                //    fs.Close();
                if (exportRequest)
                    ExportRequest(file);
            };

            Try(action, retry, final);

            return ret;
        }
示例#2
0
文件: Http.cs 项目: labeuze/source
        public bool LoadToFile(string file, bool exportRequest = false)
        {
            bool ret = false;
            FileStream fs = null;
            try
            {
                if (__trace)
                    pb.Trace.WriteLine("Http.LoadToFile()");
                Open();
                zfile.CreateFileDirectory(file);
                fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read);

                //DateTime dtFirstCatch = new DateTime(0);
                DateTime dtFirstCatch = DateTime.Now;
                while (true)
                {
                    try
                    {
                        //if (_abortTransfer)
                        //{
                        //    ret = false;
                        //    break;
                        //}
                        StreamTransfer streamTransfer = new StreamTransfer();
                        streamTransfer.SourceLength = _resultContentLength;
                        streamTransfer.Progress.ProgressChanged += new Progress.ProgressChangedEventHandler(StreamTransferProgressChange);
                        ret = streamTransfer.Transfer(_stream, fs);
                        _webRequestDuration = DateTime.Now - _webRequestTime;
                        if (exportRequest)
                            ExportRequest(file);
                        break;
                    }
                    catch (Exception ex)
                    {
                        if (ex is IOException)
                            throw;
                        if (ex is ThreadAbortException)
                            throw;
                        if (_loadRetryTimeout == 0)
                            throw;

                        if (dtFirstCatch.Ticks == 0)
                        {
                            dtFirstCatch = DateTime.Now;
                        }
                        else if (_loadRetryTimeout != -1)
                        {
                            dtFirstCatch = DateTime.Now;
                            TimeSpan ts = DateTime.Now.Subtract(dtFirstCatch);
                            if (ts.Seconds > _loadRetryTimeout)
                                throw;
                        }
                        //if (HttpRetry != null && !HttpRetry(ex))
                        //    throw;

                        Close();
                        Open();
                        FileStream fs2 = fs;
                        fs = null;
                        fs2.Close();
                        fs = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read);
                    }
                }
            }
            finally
            {
                //_abortTransfer = false;
                if (fs != null)
                    fs.Close();
                Close();
            }
            return ret;
        }