Exemplo n.º 1
0
        public static bool DownloadFile(Uri url, string destFileName, HttpWebResponse response, CancellationToken?token = null, Action <int> indicateProgress = null)
        {
            using (new ProfileSection("Download file", typeof(WebRequestHelper)))
            {
                ProfileSection.Argument("url", url);
                ProfileSection.Argument("destFileName", destFileName);
                ProfileSection.Argument("response", response);
                ProfileSection.Argument("token", token);
                ProfileSection.Argument("indicateProgress", indicateProgress);

                using (var responseStream = response.GetResponseStream())
                {
                    try
                    {
                        Assert.ArgumentNotNull(responseStream, "responseStream");
                        responseStream.ReadTimeout = Settings.CoreWebDownloadTimeoutMinutes.Value * Minute;
                        DownloadFile(destFileName, responseStream, token, indicateProgress);

                        ProfileSection.Result("Done");
                        return(true);
                    }
                    catch (OperationCanceledException)
                    {
                        FileSystem.FileSystem.Local.File.Delete(destFileName);

                        ProfileSection.Result("Canceled");
                        return(false);
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidOperationException("Downloading failed with exception", ex);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static bool DownloadFile(Uri url, string destFileName, int?timeout = null, int?readWriteTimeout = null, string cookies = null)
        {
            using (new ProfileSection("Download file", typeof(WebRequestHelper)))
            {
                ProfileSection.Argument("url", url);
                ProfileSection.Argument("destFileName", destFileName);

                var response = RequestAndGetResponse(url, timeout, readWriteTimeout, cookies);
                using (var responseStream = response.GetResponseStream())
                {
                    try
                    {
                        DownloadFile(destFileName, responseStream);

                        ProfileSection.Result("Done");
                        return(true);
                    }
                    catch (OperationCanceledException)
                    {
                        FileSystem.FileSystem.Local.File.Delete(destFileName);

                        ProfileSection.Result("Canceled");
                        return(false);
                    }
                    catch (Exception ex)
                    {
                        throw new InvalidOperationException("Downloading failed with exception", ex);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public static object CreateObject(Type type, params object[] objects)
        {
            Assert.ArgumentNotNull(type, "type");

            using (new ProfileSection("Create object"))
            {
                ProfileSection.Argument("type", type);
                ProfileSection.Argument("objects", objects);

                return(ProfileSection.Result(Activator.CreateInstance(type, objects)));
            }
        }
Exemplo n.º 4
0
        public static void DownloadFile(string destFileName, Stream responseStream, CancellationToken?token = null, Action <int> indicateProgress = null)
        {
            using (new ProfileSection("Download file", typeof(WebRequestHelper)))
            {
                ProfileSection.Argument("destFileName", destFileName);
                ProfileSection.Argument("responseStream", responseStream);
                ProfileSection.Argument("token", token);
                ProfileSection.Argument("indicateProgress", indicateProgress);

                int bufferSize = Settings.CoreWebDownloadBufferSize.Value;
                using (var fileStream = new FileStream(destFileName, FileMode.Create, FileAccess.Write, FileShare.Read, bufferSize))
                {
                    var buffer = new byte[bufferSize];
                    while (true)
                    {
                        if (token != null)
                        {
                            ((CancellationToken)token).ThrowIfCancellationRequested();
                        }

                        int count = responseStream.Read(buffer, 0, bufferSize);

                        if (count == 0)
                        {
                            break;
                        }

                        fileStream.Write(buffer, 0, count);
                        if (indicateProgress != null)
                        {
                            indicateProgress(count);
                        }
                    }
                }
            }
        }