Пример #1
0
    private PayloadInfo GetRemotePayloadInfo(string url)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

        req.Method    = "Head";
        req.KeepAlive = false;
        HttpWebResponse response;
        PayloadInfo     payloadInfo;

        try
        {
            LegacyDownloader.Add64BitRange(req, 0L, 0L);
            response = (HttpWebResponse)req.GetResponse();
            string httpResponseHeaders = LegacyDownloader.GetHTTPResponseHeaders(response);
            this.mResponseHeaders = response.Headers;
            Logger.Warning(httpResponseHeaders);
            payloadInfo = response.StatusCode != HttpStatusCode.PartialContent ? (response.StatusCode != HttpStatusCode.OK ? new PayloadInfo(false, 0L, true) : (!httpResponseHeaders.Contains("Accept-Ranges: bytes") ? new PayloadInfo(false, response.ContentLength, false) : new PayloadInfo(true, response.ContentLength, false))) : new PayloadInfo(true, LegacyDownloader.GetSizeFromContentRange(response), false);
        }
        catch (Exception ex)
        {
            Logger.Error(ex.ToString());
            throw;
        }
        response.Close();
        return(payloadInfo);
    }
Пример #2
0
    public void DoWork(object data)
    {
        LegacyDownloader.Worker worker = (LegacyDownloader.Worker)data;
        Range           range          = worker?.Range;
        Stream          stream1        = (Stream)null;
        HttpWebResponse res            = (HttpWebResponse)null;
        Stream          stream2        = (Stream)null;

        try
        {
            Logger.Info("WorkerId {0} range.From = {1}, range.To = {2}", (object)worker.Id, (object)range.From, (object)range.To);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(worker.URL);
            req.KeepAlive = true;
            if (System.IO.File.Exists(worker.PartFileName))
            {
                stream1 = (Stream) new FileStream(worker.PartFileName, FileMode.Append, FileAccess.Write, FileShare.None);
                if (stream1.Length == range.Length)
                {
                    worker.TotalFileDownloaded = stream1.Length;
                    worker.PercentComplete     = 100;
                    Logger.Info("WorkerId {0} already downloaded", (object)worker.Id);
                    return;
                }
                worker.TotalFileDownloaded = stream1.Length;
                worker.PercentComplete     = (int)(stream1.Length * 100L / range.Length);
                Logger.Info("WorkerId {0} Resuming from range.From = {1}, range.To = {2}", (object)worker.Id, (object)(range.From + stream1.Length), (object)range.To);
                if (this.mNrWorkers > 1)
                {
                    LegacyDownloader.Add64BitRange(req, range.From + stream1.Length, range.To);
                }
            }
            else
            {
                worker.TotalFileDownloaded = 0L;
                worker.PercentComplete     = 0;
                stream1 = (Stream) new FileStream(worker.PartFileName, FileMode.Create, FileAccess.Write, FileShare.None);
                if (this.mNrWorkers > 1)
                {
                    LegacyDownloader.Add64BitRange(req, range.From + stream1.Length, range.To);
                }
            }
            req.ReadWriteTimeout = 60000;
            res = (HttpWebResponse)req.GetResponse();
            long contentLength = res.ContentLength;
            stream2 = res.GetResponseStream();
            int    count1 = 65536;
            byte[] buffer = new byte[count1];
            long   num    = 0;
            Logger.Warning(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "WorkerId {0}\n", (object)worker.Id) + LegacyDownloader.GetHTTPResponseHeaders(res));
            int count2;
            while ((count2 = stream2.Read(buffer, 0, count1)) > 0)
            {
                if (worker.Cancelled)
                {
                    throw new OperationCanceledException("Download cancelled by user.");
                }
                stream1.Write(buffer, 0, count2);
                num += (long)count2;
                worker.TotalFileDownloaded = stream1.Length;
                worker.PercentComplete     = (int)(stream1.Length * 100L / range.Length);
            }
            if (contentLength != num)
            {
                throw new Exception(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "totalContentRead({0}) != contentLength({1})", (object)num, (object)contentLength));
            }
        }
        catch (Exception ex)
        {
            worker.Exception = ex;
            Logger.Error(ex.ToString());
        }
        finally
        {
            stream2?.Close();
            res?.Close();
            if (stream1 != null)
            {
                stream1.Flush();
                stream1.Close();
            }
        }
        Logger.Info("WorkerId {0} Finished", (object)worker.Id);
    }