Esempio n. 1
0
        internal static void WriteToRequest(HttpWebRequest request, ProgressStream progressStream)
        {
            log.Info("Writing data to request synchronously.");

            if (progressStream == null)
            {
                return;
            }

            try
            {
                log.Info("Reporting progress requested.");

                var buffer = new byte[CHUNK_SIZE];
                using (var req = request.GetRequestStream())
                {
                    progressStream.Position = 0;

                    int read = 0;
                    for (int i = 0; i < progressStream.Length; i += read)
                    {
                        read = progressStream.Read(buffer, 0, CHUNK_SIZE);
                        req.Write(buffer, 0, read);
                        req.Flush(); // flushing is required or else we jump to 100% very fast
                    }

                    progressStream.ReportMaxValue();
                }
            }
            catch (WebException ex)
            {
                HandleWebException(ex);
            }
        }