private IEnumerator _download() { //避免下载完了,但是生成文件问题 if (DownloadCacheFileLenght >= ServeLength) { Complete(); yield break; } var result = webRequest.SendWebRequest(); if (IsContinueDownload) { while (webRequest.responseCode != 206) { Debug.LogError($"无法继续下载,本地大小:{DownloadCacheFileLenght}," + $"文件总大小:{ServeLength}"); yield return(null); } } while (!result.isDone) { DownloadInfo.SetDownloadLenght(DownloadCacheFileLenght + webRequest.downloadedBytes); ProgressHandle? .Invoke( new DownloadProgressInfo(webRequest.downloadedBytes - oldLenght, GetProgress(webRequest.downloadedBytes), DownloadInfo), DownloadMessageCode.GetMessage((int)DownloadMessageCodeTable.载中)); oldLenght = webRequest.downloadedBytes; yield return(null); } if (!string.IsNullOrEmpty(webRequest.error)) { ErrorHandle?.Invoke($"下载失败.\n" + $"错误信息为:{webRequest.error}\n" + $"网络状态码:{webRequest.responseCode}"); yield break; } Complete(); }
private async void _updateServerFileInfo(Dictionary <string, string> headers) { ProgressHandle?.Invoke(new DownloadProgressInfo(0), DownloadMessageCode.GetMessage((int)DownloadMessageCodeTable.开始确认远程)); #if UNITY ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback; #endif MyWebClient client = new MyWebClient(); HttpWebRequest request = WebRequest.CreateHttp(new Uri(Url)); if (headers != null) { foreach (var pair in headers) { request.Headers.Add(pair.Key, pair.Value); } } request.Timeout = TimeOut; WebResponse response; try { response = await request.GetResponseAsync(); } catch (WebException webEx) { ErrorHandle?.Invoke($"确认远程文件时出现网络错误.\n" + $"响应状态为:{webEx.Status}\n" + $"错误信息:{webEx.Message}\n" + $"堆栈信息:{webEx.StackTrace}"); return; } catch (Exception e) { ErrorHandle?.Invoke($"确认远程文件时出现错误.\n" + $"错误信息:{e.Message}\n" + $"堆栈信息:{e.StackTrace}"); return; } var contentLength = response.Headers.Get(ContentLengthHeaderName); if (string.IsNullOrEmpty(contentLength)) { ErrorHandle?.Invoke($"确认远程文件失败了,没有获取到文件大小."); return; } else { long lenght; long.TryParse(contentLength, out lenght); ServeLength = (ulong)lenght; } var p = Url.Split('/', '\\'); ServerFileName = p.Last(); ServerMd5 = client.Headers.Get(ContentMD5HeaderName); response.Close(); response.Dispose(); request.Abort(); if (string.IsNullOrEmpty(SaveFileName)) { SaveFileName = ServerFileName; } ReadServerFileInfoFile(); DownloadCacheFileLenght = GetDownloadCacheFileLenght(); CheckIsContinueDownload(); if (_updateServerFile()) { DownloadInfo.SetTotalLength(ServeLength); DownloadInfo.SetDownloadLenght(DownloadCacheFileLenght); if (IsContinueDownload) { //继续下载 ProgressHandle?.Invoke(new DownloadProgressInfo(1), DownloadMessageCode.GetMessage((int)DownloadMessageCodeTable.确认结束_继续下载)); } else { Utility.FileUtil.DeleteFile(SaveFilePath); Utility.FileUtil.DeleteFile(SaveFilePathTemp); if (DownloadCacheFileLenght == 0) { //开始下载 ProgressHandle?.Invoke(new DownloadProgressInfo(1), DownloadMessageCode.GetMessage((int)DownloadMessageCodeTable.确认结束_开始下载)); } else { //重新下载 ProgressHandle?.Invoke(new DownloadProgressInfo(1), DownloadMessageCode.GetMessage((int)DownloadMessageCodeTable.确认结束_重新下载)); } } DownloadStartHandle?.Invoke(DownloadInfo); Download(); } }