Ejemplo n.º 1
0
 private void Client_DownloadProgressChanged(object sender, DownloadEventArgs e)
 {
     this.bytesReceived   = e.bytesReceived;
     this.totalBytes      = e.totalBytes;
     this.receiveSpeed    = e.receiveSpeed;
     this.ReceiveProgress = e.ReceiveProgress;
 }
Ejemplo n.º 2
0
        private void SaveNetworkStream(Stream toStream, bool showProgress)
        {
            byte[]       buffer2    = new byte[10240];
            int          count4     = 0;
            int          startIndex = 0;
            MemoryStream ms         = new MemoryStream();

            for (int i = 0; i < 3; i++)
            {
                count4 = workStream.Read(buffer2, 0, 500);
                ms.Write(buffer2, 0, count4);
            }
            if (ms.Length == 0)
            {
                workStream.Close();
                throw new Exception("远程服务器没有响应");
            }
            buffer2 = ms.GetBuffer();
            count4  = (int)ms.Length;
            GetResponseHeader(buffer2, out startIndex);
            count4 -= startIndex;
            toStream.Write(buffer2, startIndex, count4);
            DownloadEventArgs e = default(DownloadEventArgs);

            if (responseHeaders["Content-Length"] != null)
            {
                e.totalBytes = long.Parse(responseHeaders["Content-Length"]);
            }
            else
            {
                e.totalBytes = -1L;
            }
            Stopwatch timer = new Stopwatch();

            timer.Start();
            while (!isCanceled)
            {
                if (showProgress)
                {
                    e.bytesReceived  += count4;
                    e.ReceiveProgress = (double)e.bytesReceived / (double)e.totalBytes;
                    byte[] tempBuffer = new byte[count4];
                    Array.Copy(buffer2, startIndex, tempBuffer, 0, count4);
                    e.receivedBuffer = tempBuffer;
                    double t = ((double)timer.ElapsedMilliseconds + 0.1) / 1000.0;
                    e.receiveSpeed = (double)e.bytesReceived / t;
                    startIndex     = 0;
                    if (this.DownloadProgressChanged != null)
                    {
                        this.DownloadProgressChanged(this, e);
                    }
                }
                count4 = workStream.Read(buffer2, 0, buffer2.Length);
                toStream.Write(buffer2, 0, count4);
                if (count4 <= 0)
                {
                    break;
                }
            }
            timer.Stop();
            if (responseHeaders["Content-Length"] != null)
            {
                toStream.SetLength(long.Parse(responseHeaders["Content-Length"]));
            }
            toStream.Position = 0L;
            workStream.Close();
            clientSocket.Close();
        }