public ServerFileInfo GetFileInfo(string url) { HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; HttpWebResponse response = request.GetResponse() as HttpWebResponse; ServerFileInfo fileinfo = new ServerFileInfo(); fileinfo.ResponseUri = response.ResponseUri; fileinfo.FileType = response.ContentType; fileinfo.FileSize = response.ContentLength; fileinfo.LastModified = response.LastModified; fileinfo.Method = response.Method; fileinfo.Server = response.Server; fileinfo.AcceptRange = string.Compare(response.Headers["Accept-Ranges"], "bytes", true) == 0; fileinfo.StatueCode = response.StatusCode; fileinfo.CharacterSet = response.CharacterSet; fileinfo.ContentEncoding = response.ContentEncoding; fileinfo.ProtocolVersion = response.ProtocolVersion.ToString(); fileinfo.StatusDescription = response.StatusDescription; response.Close(); return fileinfo; }
/// <summary> /// 开始暂停的任务 /// </summary> private void StartPrepared() { int errorTime = 0; try { do { errorTime++; try { serverFileInfo = httpManager.GetFileInfo(url); break; } catch { if (errorTime < Settings.Default.MaxConnect) { SetState(DownloadState.WaitForConnect); Thread.Sleep(TimeSpan.FromMilliseconds(Settings.Default.RetryDelay)); } else { return; } } } while (true); } finally { SetState(DownloadState.prepared); } StartSegments(); }
private void StartNeedToPrepare() { int errorTime = 0; SetState(DownloadState.Preparing); do { if (state == DownloadState.pausing) { SetState(DownloadState.NeedToPrepare); return; } SetState(DownloadState.Preparing); errorTime++; try { serverFileInfo = httpManager.GetFileInfo(url); if ((HttpStatusCode)serverFileInfo.StatueCode == HttpStatusCode.OK) { break; } } catch (ThreadAbortException) { SetState(DownloadState.NeedToPrepare); return; } catch (Exception ex) { if (errorTime < Settings.Default.MaxConnect) { SetState(DownloadState.WaitForConnect); Thread.Sleep(TimeSpan.FromMilliseconds(Settings.Default.RetryDelay)); } else { SetState(DownloadState.NeedToPrepare); return; } } } while (true); try { CreateSegments(); } catch(ThreadAbortException) { throw; } catch { SetState(DownloadState.EndedWithError); } }