Exemplo n.º 1
0
        public void GetHTTPFile(string url, string file, int tiemout, int maxtries)
        {
            if (File.Exists(file))
            {
                File.Delete(file);
            }
            Stream output = null;

            if (file != null)
            {
                output = File.Create(file);
            }
            bool close = true;
            int  tries = 0;

            while (true)
            {
                try
                {
                    tries++;
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Timeout = tiemout;
                    request.Proxy   = null;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                    Stream resStream = response.GetResponseStream();

                    if ((this.updwindow != null) && (tries == 1))
                    {
                        this.updwindow.Invoke(updwindow.newprogress, "");
                    }

                    int    count     = 0;
                    int    totalread = 0;
                    byte[] buffer    = new byte[8192];
                    do
                    {
                        count     = resStream.Read(buffer, 0, buffer.Length);
                        totalread = totalread + count;
                        if (response.ContentLength > 0)
                        {
                            if (this.updwindow != null)
                            {
                                this.updwindow.Invoke(updwindow.newprogress, Math.Round(((double)totalread / (double)response.ContentLength) * 100D, 3).ToString() + " %");
                            }
                        }
                        if ((count != 0) && (output != null))
                        {
                            output.Write(buffer, 0, count);
                        }
                    }while (count > 0);
                    break;
                }
                catch (Exception e)
                {
                    close = false;
                    if (output != null)
                    {
                        output.Close();
                    }
                    if ((this.updwindow != null) && (tries == 1))
                    {
                        this.updwindow.Invoke(updwindow.newprogress, "");
                    }
                    if (tries > maxtries)
                    {
                        throw new Exception(e.Message, e);
                    }
                    else
                    {
                        File.Delete(file);
                        if (file != null)
                        {
                            output = File.Create(file);
                        }
                        close = true;
                    }
                    ConsoleLogger.LogData("Download failo: " + e.Message, "GetHTTPFile");
                }
            }
            if (close)
            {
                if (output != null)
                {
                    output.Close();
                }
                if (this.updwindow != null)
                {
                    this.updwindow.Invoke(updwindow.newprogress, "");
                }
            }
        }