private static async Task <StorageFile> DownloadFile(StorageFile destinationFile, Uri source, Addon addon, IProgressable progressable) { try { var result = Http.WebHttpClient.GetAsync(source); var downloadProgessHandler = new DownloadProgressHandler() { Progressable = progressable != null ? progressable : addon }; result.Progress = downloadProgessHandler.DownloadProgressCallback; using (var filestream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite)) { var res = await result; await res.Content.WriteToStreamAsync(filestream); await filestream.FlushAsync(); } } catch (Exception ex) { Debug.WriteLine("[ERROR] DownloadFile. " + ex.Message + " " + ex.StackTrace); } return(destinationFile); }
public DownloadCommand(IModelItem owner) : base(owner) { this.Progress = -1f; this.m_progressHandler = new DownloadProgressHandler(this.OnProgressChanged); this.m_myLock = new object(); }
public WebDownload(String url, DownloadProgressHandler callback) { this.url = url; this.progressCB = callback; info.ProgressCallback += this.progressCB; StatusManager.Instance.SetTaskPercentCompletion(url.GetHashCode(), 0); }
public static byte[] DownloadData(string url, DownloadProgressHandler progressCallback, object callbackParam, IEnumerable <KeyValuePair <string, string> > additionalHeaders = null) { if (progressCallback != null) { bool cancelled = false; Exception error = null; byte[] data = null; bool isFinished = false; using (WebClient webClient = GetConfiguredWebClient(additionalHeaders)) { webClient.DownloadProgressChanged += delegate(Object sender, DownloadProgressChangedEventArgs e) { if (!progressCallback(e.ProgressPercentage, callbackParam)) { webClient.CancelAsync(); } }; webClient.DownloadDataCompleted += delegate(Object sender, DownloadDataCompletedEventArgs e) { error = e.Error; if (error == null) { data = e.Result; } cancelled = e.Cancelled; isFinished = true; }; webClient.DownloadDataAsync(new Uri(url)); while (!isFinished) { Thread.Sleep(60); } } if (cancelled) { throw new OperationCanceledException(); } if (error != null) { throw error; } return(data); } else { using (WebClient webClient = GetConfiguredWebClient(additionalHeaders)) { return(webClient.DownloadData(url)); } } }
public static DownloadTaskErrorCode GetByUrl( string url, string hostName, string dest, int timeout, EventHandler callbackComplete, DownloadProgressHandler callbackProgress, out string errMsg) { DownloadTaskErrorCode errCode = DownloadTaskErrorCode.Success; #if USE_CURL_DOWNLOADER FileDownloaderEx downloader = new FileDownloaderEx(); #else FileDownloader downloader = new FileDownloader(); #endif if (callbackComplete != null) { downloader.DownloadComplete += callbackComplete; } if (callbackProgress != null) { downloader.ProgressChanged += callbackProgress; } errMsg = ""; try { downloader.Download(url, hostName, dest, timeout); } catch (WebException webException) { errMsg = webException.Message; errCode = DownloadTaskErrorCode.NetworkError; } catch (ArgumentException argException) { errMsg = argException.Message; errCode = DownloadTaskErrorCode.UrlArgError; } catch (IOException ioException) { errMsg = ioException.Message; errCode = DownloadTaskErrorCode.IOError; } catch (Exception e) { errMsg = e.Message; errCode = DownloadTaskErrorCode.Unknown; } return(errCode); }
public void Dispose() { this.disposed = true; this.info.ProgressCallback -= this.progressCB; this.downloadTimer.Dispose(); this.info.Dispose(); this.progressCB = null; this.allDownloadedData = null; this.downloadTimer = null; this.info = null; }
public byte[] Download(string url, DownloadProgressHandler progressCB) { // Ensure flag set correctly. allDone.Reset(); // Get the URI from the command line. Uri httpSite = new Uri(url); // Create the request object. WebRequest req = WebRequest.Create(httpSite); try { // Use the current user in case an NTLM Proxy or similar is used. // wr.Proxy = WebProxy.GetDefaultProxy(); req.Proxy.Credentials = CredentialCache.DefaultCredentials; } catch (Exception) {} // Create the state object. DownloadInfo info = new DownloadInfo(); // Put the request into the state object so it can be passed around. info.Request = req; // Assign the callbacks info.ProgressCallback += progressCB; // Issue the async request. IAsyncResult r = (IAsyncResult)req.BeginGetResponse(new AsyncCallback(ResponseCallback), info); // Wait until the ManualResetEvent is set so that the application // does not exit until after the callback is called. allDone.WaitOne(); // Pass back the downloaded information. if (info.useFastBuffers) { return(info.dataBufferFast); } else { byte[] data = new byte[info.dataBufferSlow.Count]; for (int b = 0; b < info.dataBufferSlow.Count; b++) { data[b] = (byte)info.dataBufferSlow[b]; } return(data); } }
/// <summary> /// Aborts the current download. /// </summary> public void Cancel() { CompleteCallback = null; ProgressCallback = null; if (dlThread != null && dlThread != Thread.CurrentThread) { if (dlThread.IsAlive) { dlThread.Abort(); } dlThread = null; } }
public byte[] Download(string url, DownloadProgressHandler progressCB) { // Ensure flag set correctly. allDone.Reset(); // Get the URI from the command line. Uri httpSite = new Uri(url); // Create the request object. WebRequest req = WebRequest.Create(httpSite); try { // Use the current user in case an NTLM Proxy or similar is used. // wr.Proxy = WebProxy.GetDefaultProxy(); req.Proxy.Credentials = CredentialCache.DefaultCredentials; } catch (Exception) {} // Create the state object. DownloadInfo info = new DownloadInfo(); // Put the request into the state object so it can be passed around. info.Request = req; // Assign the callbacks info.ProgressCallback += progressCB; // Issue the async request. IAsyncResult r = (IAsyncResult)req.BeginGetResponse(new AsyncCallback(ResponseCallback), info); // Wait until the ManualResetEvent is set so that the application // does not exit until after the callback is called. allDone.WaitOne(); // Pass back the downloaded information. if (info.useFastBuffers) { return info.dataBufferFast; } else { byte[] data = new byte[info.dataBufferSlow.Count]; for (int b = 0; b < info.dataBufferSlow.Count; b++) { data[b] = (byte)info.dataBufferSlow[b]; } return data; } }
public static void DownloadFile(string url, string filePath, DownloadProgressHandler progressCallback, object callbackParam, IEnumerable <KeyValuePair <string, string> > additionalHeaders = null) { if (progressCallback != null) { Exception error = null; bool cancelled = false; bool isFinished = false; using (WebClient webClient = GetConfiguredWebClient(additionalHeaders)) { webClient.DownloadProgressChanged += delegate(Object sender, DownloadProgressChangedEventArgs e) { if (!progressCallback(e.ProgressPercentage, callbackParam)) { webClient.CancelAsync(); } }; webClient.DownloadFileCompleted += delegate(Object sender, AsyncCompletedEventArgs e) { error = e.Error; cancelled = e.Cancelled; isFinished = true; }; webClient.DownloadFileAsync(new Uri(url), filePath); while (!isFinished) { Thread.Sleep(60); } } if (cancelled) { throw new OperationCanceledException(); } if (error != null) { throw error; } } else { using (WebClient webClient = GetConfiguredWebClient(additionalHeaders)) { webClient.DownloadFile(url, filePath); } } }
public byte[] Download(string url, string parameters, DownloadProgressHandler progressCB) { // Ensure flag set correctly. allDone.Reset(); // Get the URI from the command line. Uri httpSite = new Uri(url); // Create the request object. WebRequest req = WebRequest.Create(httpSite); req.Method = "POST"; req.ContentType = @"application/x-www-form-urlencoded"; //byte[] bytes = System.Text.Encoding.ASCII.GetBytes(parameters); //req.ContentLength = bytes.Length; // Create the state object. DownloadInfo info = new DownloadInfo(); // Put the request into the state object so it can be passed around. info.Request = req; // Assign the callbacks info.ProgressCallback += progressCB; // Issue the async request. IAsyncResult r = (IAsyncResult)req.BeginGetResponse(new AsyncCallback(ResponseCallback), info); // Wait until the ManualResetEvent is set so that the application // does not exit until after the callback is called. allDone.WaitOne(); // Pass back the downloaded information. if (info.useFastBuffers) { return(info.dataBufferFast); } else { byte[] data = new byte[info.dataBufferSlow.Count]; for (int b = 0; b < info.dataBufferSlow.Count; b++) { data[b] = (byte)info.dataBufferSlow[b]; } return(data); } }
public void Dispose() { if (this.ResponseStream != null) { this.Request.Abort(); //this.ResponseStream.Close(); this.ResponseStream.Dispose(); } this.BufferRead = null; this.Request = null; this.dataBufferFast = null; this.dataBufferSlow = null; this.ResponseStream = null; this.ProgressCallback = null; }
/// <summary> /// Aborts the current download. /// </summary> public void Cancel() { CompleteCallback = null; ProgressCallback = null; if (dlThread != null && dlThread != Thread.CurrentThread) { if (dlThread.IsAlive) { stopFlag = true; if (!dlThread.Join(500)) { dlThread.Abort(); } } dlThread = null; } }
public static DownloadTaskErrorCode GetByUrl( string url, string hostName, string dest, int timeout, EventHandler callbackComplete, DownloadProgressHandler callbackProgress, out string errMsg) { return(DownloadMan.GetByUrl( url, hostName, dest, timeout, callbackComplete, callbackProgress, out errMsg)); }
/// <summary> /// Aborts the current download. /// </summary> public void Cancel() { CompleteCallback = null; ProgressCallback = null; if (dlThread != null && dlThread != Thread.CurrentThread) { if (dlThread.IsAlive) { Log.Write(Log.Levels.Verbose, "WebDownload.Cancel() : stopping download thread..."); stopFlag = true; if (!dlThread.Join(500)) { Log.Write(Log.Levels.Warning, "WebDownload.Cancel() : download thread refuses to die, forcing Abort()"); dlThread.Abort(); } } dlThread = null; } }
/// <summary> /// HTTP downloads to memory. /// </summary> /// <param name="progressCallback">The progress callback.</param> public void DownloadMemory(DownloadProgressHandler progressCallback) { ProgressCallback += progressCallback; DownloadMemory(); }
/// <summary> /// Updates frame progress bar /// </summary> void UpdateProgressBarFrame(int bytesSoFar, int bytesTotal) { // Make sure we're on the right thread if( this.InvokeRequired ) { // Update progress asynchronously DownloadProgressHandler dlgt = new DownloadProgressHandler(UpdateProgressBarFrame); this.BeginInvoke(dlgt, new object[] { bytesSoFar, bytesTotal }); return; } if(bytesSoFar < 0) bytesSoFar = 0; if(bytesTotal < 0) bytesTotal = 0; if(bytesTotal<bytesSoFar) bytesTotal=bytesSoFar; this.progressBarFrame.Maximum = bytesTotal; this.progressBarFrame.Value = bytesSoFar<=bytesTotal ? bytesSoFar : bytesTotal; }
/// <summary> /// Updates progress bar (thread safe) /// </summary> private void updateProgressBar(int value, int maximum) { // Make sure we're on the right thread if( this.InvokeRequired ) { // Update progress asynchronously DownloadProgressHandler dlgt = new DownloadProgressHandler(updateProgressBar); this.BeginInvoke(dlgt, new object[] { value, maximum }); return; } if(value < 0) value = 0; if(maximum < 0) maximum = 0; this.progressBar.Maximum = maximum; this.progressBar.Value = value<=maximum ? value : maximum; }
public static async Task ExecuteAsync(HttpClient httpClient, string downloadPath, string destinationPath, DownloadProgressHandler progress, Func <HttpRequestMessage> requestMessageBuilder = null) { requestMessageBuilder ??= GetDefaultRequestBuilder(downloadPath); var download = new HttpClientDownloadWithProgress(httpClient, destinationPath, requestMessageBuilder); download.ProgressChanged += progress; await download.StartDownload(); download.ProgressChanged -= progress; }
/// <summary> /// Updates progress bar (thread safe) /// </summary> private void updateCurrentProgressBar(int bytesSoFar, int bytesTotal) { // Make sure we're on the right thread if( this.InvokeRequired ) { // Update progress asynchronously DownloadProgressHandler dlgt = new DownloadProgressHandler(updateCurrentProgressBar); this.BeginInvoke(dlgt, new object[] { bytesSoFar, bytesTotal }); return; } if(bytesSoFar < 0) bytesSoFar = 0; if(bytesTotal < 0) bytesTotal = 0; this.progressBarStatus.Maximum = bytesTotal; this.progressBarStatus.Value = bytesSoFar<=bytesTotal ? bytesSoFar : bytesTotal; }
public byte[] Download(DownloadInfo info, DownloadProgressHandler progressCallback) { //LibSys.StatusBar.Trace("IP: downloading - " + info.baseName + " == " + info.strUrl); allDoneLast = allDone; // Ensure flag set correctly. allDone.Reset(); m_baseName = info.baseName; // Get the URI from the command line. Uri httpSite = new Uri(info.strUrl); // Create the request object. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(httpSite); if (Project.suspendKeepAlive) { req.KeepAlive = false; } /* * see Project.ApplyGlobalHTTPProxy() * * if(Project.useProxy) * { * WebProxy proxy = new WebProxy(Project.proxyServer, Project.proxyPort); * req.Proxy = proxy; * } */ #if DEBUG LibSys.StatusBar.Trace("IP: downloading - " + info.baseName + " == " + info.strUrl + " after proxy: " + req.Proxy.GetProxy(httpSite)); #endif // Put the request into the state object so it can be passed around. info.Request = req; // Assign the callbacks info.ProgressCallback += progressCallback; /* * // this is to debug ThreadPool and ProgressMonitor * if(info.strUrl.IndexOf("ashx") != -1) * { * info.dataLength = 20; * int ccc = 0; * while (ccc++ < 20) * { * Thread.Sleep(500); * if ( info.ProgressCallback != null ) * { * //LibSys.StatusBar.Trace("IP: loading... " + info.baseName); * info.bytesProcessed = ccc; * info.ProgressCallback(info); * } * } * LibSys.StatusBar.Trace("OK: finished " + info.baseName); * return null; * } */ // Issue the async request. IAsyncResult r = (IAsyncResult)req.BeginGetResponse(new AsyncCallback(ResponseCallback), info); // Wait until the ManualResetEvent is set so that the application // does not exit until after the callback is called. bool hasSignal; if (m_timeOutMs == 0) { hasSignal = allDone.WaitOne(); } else { #if DEBUG //LibSys.StatusBar.Trace("IP: WebDownload:Download() at WaitOne - " + DateTime.Now + " - " + m_baseName + " " + info.strUrl); #endif hasSignal = allDone.WaitOne(m_timeOutMs, false); #if DEBUG //LibSys.StatusBar.Trace("IP: WebDownload:Download() after WaitOne - " + DateTime.Now + " - " + hasSignal + " " + m_baseName + " " + info.strUrl); #endif } if (!hasSignal) { allDone.Set(); info.hasTimedOut = true; return(null); } // Pass back the downloaded information. if (info.useFastBuffers) { return(info.dataBufferFast); } else { byte[] data = new byte[info.dataBufferSlow.Count]; for (int b = 0; b < info.dataBufferSlow.Count; b++) { data[b] = (byte)info.dataBufferSlow[b]; } return(data); } }
public static async Task <bool> DownloadApkAsync(string assetUrl, string downloadFilePath, DownloadProgressHandler progressHandler) { try { // https://docs.github.com/en/rest/reference/repos#get-a-release-asset await DownloadWithProgress.ExecuteAsync(HttpClients.General, assetUrl, downloadFilePath, progressHandler, () => { var requestMessage = new HttpRequestMessage(HttpMethod.Get, assetUrl); requestMessage.Headers.Accept.TryParseAdd("application/octet-stream"); return(requestMessage); }); return(true); } catch (Exception e) { Log.Error(e, "An error occured while downloading the file"); return(false); } }
public byte[] Download(string url, string parameters, DownloadProgressHandler progressCB) { // Ensure flag set correctly. allDone.Reset(); // Get the URI from the command line. Uri httpSite = new Uri(url); // Create the request object. WebRequest req = WebRequest.Create(httpSite); req.Method = "POST"; req.ContentType = @"application/x-www-form-urlencoded"; //byte[] bytes = System.Text.Encoding.ASCII.GetBytes(parameters); //req.ContentLength = bytes.Length; // Create the state object. DownloadInfo info = new DownloadInfo(); // Put the request into the state object so it can be passed around. info.Request = req; // Assign the callbacks info.ProgressCallback += progressCB; // Issue the async request. IAsyncResult r = (IAsyncResult)req.BeginGetResponse(new AsyncCallback(ResponseCallback), info); // Wait until the ManualResetEvent is set so that the application // does not exit until after the callback is called. allDone.WaitOne(); // Pass back the downloaded information. if (info.useFastBuffers) return info.dataBufferFast; else { byte[] data = new byte[info.dataBufferSlow.Count]; for (int b = 0; b < info.dataBufferSlow.Count; b++) data[b] = (byte)info.dataBufferSlow[b]; return data; } }
public byte[] Download( DownloadInfo info, DownloadProgressHandler progressCallback ) { //LibSys.StatusBar.Trace("IP: downloading - " + info.baseName + " == " + info.strUrl); allDoneLast = allDone; // Ensure flag set correctly. allDone.Reset(); m_baseName = info.baseName; // Get the URI from the command line. Uri httpSite = new Uri(info.strUrl); // Create the request object. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(httpSite); if(Project.suspendKeepAlive) { req.KeepAlive = false; } /* * see Project.ApplyGlobalHTTPProxy() * if(Project.useProxy) { WebProxy proxy = new WebProxy(Project.proxyServer, Project.proxyPort); req.Proxy = proxy; } */ #if DEBUG LibSys.StatusBar.Trace("IP: downloading - " + info.baseName + " == " + info.strUrl + " after proxy: " + req.Proxy.GetProxy(httpSite)); #endif // Put the request into the state object so it can be passed around. info.Request = req; // Assign the callbacks info.ProgressCallback += progressCallback; /* // this is to debug ThreadPool and ProgressMonitor if(info.strUrl.IndexOf("ashx") != -1) { info.dataLength = 20; int ccc = 0; while (ccc++ < 20) { Thread.Sleep(500); if ( info.ProgressCallback != null ) { //LibSys.StatusBar.Trace("IP: loading... " + info.baseName); info.bytesProcessed = ccc; info.ProgressCallback(info); } } LibSys.StatusBar.Trace("OK: finished " + info.baseName); return null; } */ // Issue the async request. IAsyncResult r = (IAsyncResult) req.BeginGetResponse(new AsyncCallback(ResponseCallback), info); // Wait until the ManualResetEvent is set so that the application // does not exit until after the callback is called. bool hasSignal; if(m_timeOutMs == 0) { hasSignal = allDone.WaitOne(); } else { #if DEBUG //LibSys.StatusBar.Trace("IP: WebDownload:Download() at WaitOne - " + DateTime.Now + " - " + m_baseName + " " + info.strUrl); #endif hasSignal = allDone.WaitOne(m_timeOutMs, false); #if DEBUG //LibSys.StatusBar.Trace("IP: WebDownload:Download() after WaitOne - " + DateTime.Now + " - " + hasSignal + " " + m_baseName + " " + info.strUrl); #endif } if(!hasSignal) { allDone.Set(); info.hasTimedOut = true; return null; } // Pass back the downloaded information. if ( info.useFastBuffers ) { return info.dataBufferFast; } else { byte[] data = new byte[ info.dataBufferSlow.Count ]; for ( int b=0; b<info.dataBufferSlow.Count; b++ ) { data[b] = (byte) info.dataBufferSlow[b]; } return data; } }
/// <summary> /// Aborts the current download. /// </summary> public void Cancel() { CompleteCallback = null; ProgressCallback = null; if (dlThread!=null && dlThread != Thread.CurrentThread) { if (dlThread.IsAlive) { Log.Write(Log.Levels.Verbose, "WebDownload.Cancel() : stopping download thread..."); stopFlag = true; if (!dlThread.Join(500)) { Log.Write(Log.Levels.Warning, "WebDownload.Cancel() : download thread refuses to die, forcing Abort()"); dlThread.Abort(); } } dlThread = null; } }