Ejemplo n.º 1
0
 private bool DownloadFileFromWeb(string address, string filename, bool allowredirect, bool autounzip, string password = "")
 {
     // donwloads file from a web resource
     try
     {
         // get web cration time
         DateTime webcreationtime = GetWebCreationTimeUtc(address, allowredirect);
         // download file and check for errors and uri identical to request
         // do not use WebClient.Download for this!
         var request = (HttpWebRequest)WebRequest.Create(address);
         // allow redirect 2017/12/06 DL2ALF
         request.AllowAutoRedirect = allowredirect;
         request.Method            = "GET";
         using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
         {
             HttpStatusCode status = response.StatusCode;
             if ((status == HttpStatusCode.OK) && (response.ResponseUri == new Uri(address)))
             {
                 using (var responseStream = response.GetResponseStream())
                 {
                     using (var fileToDownload = new System.IO.FileStream(filename, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite))
                     {
                         responseStream.CopyTo(fileToDownload);
                     }
                 }
             }
         }
         // set creation time
         if (File.Exists(filename))
         {
             File.SetCreationTime(filename, webcreationtime);
             File.SetLastWriteTime(filename, webcreationtime);
             // unzip the file content if enabled
             if (autounzip && (Path.GetExtension(filename).ToLower() == ".zip"))
             {
                 Console.WriteLine("[DownloadFileFromWeb] Trying to unzip downloaded file: " + filename);
                 return(ZIP.UncompressFile(filename, 60, password));
             }
             Console.WriteLine("[DownloadFileFromWeb] Downloading file from address finished: " + address);
             return(true);
         }
     }
     catch (Exception ex)
     {
         if (ex is WebException)
         {
             Console.WriteLine("[DownloadFileFromWeb] WebException while reading address: " + address + "\n" + "URI of orginal request=" + address + "\n" + "URI of responding server=" + "\n" + ex.ToString());
         }
         else
         {
             Console.WriteLine("[DownloadFileFromWeb] Error while reading address: " + address + "\n" + ex.ToString());
         }
     }
     return(false);
 }
Ejemplo n.º 2
0
        private DOWNLOADFILESTATUS DownloadFileFromWebIfNewer(string address, string filename, bool allowredirect, bool autounzip, string password = "")
        {
            DateTime filecreationtime;
            DateTime webcreationtime;

            if (File.Exists(filename))
            {
                filecreationtime = File.GetCreationTimeUtc(filename);
            }
            else
            {
                filecreationtime = DateTime.MinValue;
            }
            webcreationtime = GetWebCreationTimeUtc(address, allowredirect);
            // nothing found on web
            if (webcreationtime == DateTime.MinValue)
            {
                return(DOWNLOADFILESTATUS.NOTFOUND);
            }
            if (webcreationtime > filecreationtime)
            {
                //  web content is newer --> download and do not unzip
                if (!DownloadFileFromWeb(address, filename, allowredirect, false, password))
                {
                    return(DOWNLOADFILESTATUS.ERROR);
                }
            }
            // unzip the file if enabled and content is a ZIP-file
            if (autounzip && (Path.GetExtension(filename).ToLower() == ".zip"))
            {
                if (!ZIP.UncompressFile(filename, 60, password))
                {
                    return(DOWNLOADFILESTATUS.ERROR);
                }
            }
            // set the return value
            if (webcreationtime > filecreationtime)
            {
                return(DOWNLOADFILESTATUS.NEWER);
            }
            else
            {
                return(DOWNLOADFILESTATUS.NOTNEWER);
            }
        }