public void Cancel() { Cancelled = true; ToFileDownloadHandler fileDownloadHandler = webRequest.downloadHandler as ToFileDownloadHandler; if (fileDownloadHandler != null) { fileDownloadHandler.Cancel(); } webRequest.Abort(); }
public void Dispose() { if (webRequest != null) { ToFileDownloadHandler fileDownloadHandler = webRequest.downloadHandler as ToFileDownloadHandler; if (fileDownloadHandler != null) { fileDownloadHandler.DisposeStream(); } webRequest.Dispose(); webRequest = null; } }
private IEnumerator DownloadFileCoroutine() { ToFileDownloadHandler downloadHandler; try { downloadHandler = new ToFileDownloadHandler(new byte[64 * 1024], pendingDownloadPath); } catch (Exception e) { if (OnDownloadFileComplete != null) { OnDownloadFileComplete(false, e, pendingUserState); } yield break; } try { webRequest = new UnityWebRequest(pendingDownloadUrl, UnityWebRequest.kHttpVerbGET, downloadHandler, null); string sentCookie = cookies[pendingDownloadUrl]; if (sentCookie != null) { webRequest.SetRequestHeader("cookie", sentCookie); } #if UNITY_2017_2_OR_NEWER webRequest.SendWebRequest(); #else webRequest.Send(); #endif } catch (Exception e) { downloadHandler.DisposeStream(); if (webRequest != null) { webRequest.Dispose(); webRequest = null; } if (OnDownloadFileComplete != null) { OnDownloadFileComplete(false, e, pendingUserState); } yield break; } while (!webRequest.isDone) { if (OnDownloadFileProgressChange != null) { OnDownloadFileProgressChange((long)webRequest.downloadedBytes, ((ToFileDownloadHandler)webRequest.downloadHandler).ContentLength); } yield return(null); } downloadHandler.DisposeStream(); #if UNITY_2017_1_OR_NEWER bool webRequestError = webRequest.isHttpError || webRequest.isNetworkError; #else bool webRequestError = webRequest.isError; #endif bool cancelled = downloadCancelled && webRequestError; if (!cancelled) { if (OnDownloadFileProgressChange != null) { long contentLength = ((ToFileDownloadHandler)webRequest.downloadHandler).ContentLength; OnDownloadFileProgressChange(contentLength, contentLength); } } string receivedCookie = webRequest.GetResponseHeader("set-cookie"); if (!string.IsNullOrEmpty(receivedCookie)) { cookies[webRequest.url] = receivedCookie; } if (OnDownloadFileComplete != null) { Exception error = webRequestError ? new Exception(webRequest.error) : null; OnDownloadFileComplete(cancelled, error, pendingUserState); } webRequest.Dispose(); webRequest = null; }