public static Task <DownloadFileRequest> Create(string url, bool noCache)
        {
            var response = WebRequestHelper.GetResponse(
                () => (HttpWebRequest)WebRequest.Create(url),
                r => {
                if (noCache)
                {
                    r.Headers ["Pragma"] = "no-cache";
                }
            }
                );

            var request = new WebRequestDownloadFileRequest {
                response = response,
                stream   = response.GetResponseStream()
            };

            return(Task.FromResult <DownloadFileRequest> (request));
        }
Example #2
0
        internal string DownloadFile(IProgressMonitor monitor, string url)
        {
            if (url.StartsWith("file://", StringComparison.Ordinal))
            {
                string tmpfile = Path.GetTempFileName();
                string path    = new Uri(url).LocalPath;
                File.Delete(tmpfile);
                File.Copy(path, tmpfile);
                return(tmpfile);
            }

            string     file = null;
            FileStream fs   = null;
            Stream     s    = null;

            try {
                monitor.BeginTask("Requesting " + url, 2);
                var resp = WebRequestHelper.GetResponse(
                    () => (HttpWebRequest)WebRequest.Create(url),
                    r => r.Headers ["Pragma"] = "no-cache"
                    );
                monitor.Step(1);
                monitor.BeginTask("Downloading " + url, (int)resp.ContentLength);

                file = Path.GetTempFileName();
                fs   = new FileStream(file, FileMode.Create, FileAccess.Write);
                s    = resp.GetResponseStream();
                byte[] buffer = new byte [4096];

                int n;
                while ((n = s.Read(buffer, 0, buffer.Length)) != 0)
                {
                    monitor.Step(n);
                    fs.Write(buffer, 0, n);
                    if (monitor.IsCancelRequested)
                    {
                        throw new InstallException("Installation cancelled.");
                    }
                }
                fs.Close();
                s.Close();
                return(file);
            } catch {
                if (fs != null)
                {
                    fs.Close();
                }
                if (s != null)
                {
                    s.Close();
                }
                if (file != null)
                {
                    File.Delete(file);
                }
                throw;
            } finally {
                monitor.EndTask();
                monitor.EndTask();
            }
        }