示例#1
0
        /// <summary>
        /// 接收到数据
        /// </summary>
        /// <param name="Sender"></param>
        /// <param name="e"></param>
        private void x_DataReceive(HttpWebClient Sender, DownLoadEventArgs e)
        {
            tCountdown.Stop();
            try
            {
                if (!b)
                {
                    lock (_SyncLockObject)
                    {
                        if (!b)
                        {
                            //Console.Write(DateTime.Now.ToString() + " 已接收数据: ");
                            b = true;
                        }
                    }
                }
                string f = e.DownloadState.FileName;
                if (e.DownloadState.AttachmentName != null)
                {
                    f = Path.GetDirectoryName(f) + @"\" + e.DownloadState.AttachmentName;
                }

                _File = f;

                using (var sw = new FileStream(f, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    sw.Position = e.DownloadState.Position;
                    sw.Write(e.DownloadState.Data, 0, e.DownloadState.Data.Length);
                    sw.Close();
                }
                string s = DateTime.Now.ToLongTimeString();
                lock (_SyncLockObject)
                {
                    i += e.DownloadState.Data.Length;
                    //int aa = int.Parse(((double) i*100/Sender.FileLength).ToString());
                    string aa = i + " / " + Sender.FileLength + " 字节数据 ";
                    string bb = "(" + ((double)i * 100 / Sender.FileLength).ToString("0.000") + "%)";

                    //pBar.Dispaly(aa, bb);

                    //Console.Write("(" + ((double)i * 100 / Sender.FileLength).ToString("0.000") + "%)");//12:12:12
                    //Console.Write(bs + "\b\b\b\b\b\b\b\b\b\b" + i + " / " + Sender.FileLength + " 字节数据 " + bb + DigiLenth(bb) + s);
                    //bs = new string('\b', Digits(i) + 3 + Digits(Sender.FileLength) + s.Length);

                    Console.Write(aa + bb + s + DigiLenth(aa + bb + s) + "\b\b\b\b");

                    if (i == Sender.FileLength)
                    {
                        eventX.Set();
                    }
                }
            }
            catch
            {
            }
            tCountdown.Start();//重新计时
        }
示例#2
0
 private void OnDataReceive(DownLoadEventArgs e)
 {
     //触发数据到达事件
     DataReceive(this, e);
 }
示例#3
0
        internal byte[] ResponseAsBytes(string RequestURL, WebResponse Response, long Length, string FileName)
        {
            string a    = null; //AttachmentName
            int    P    = 0;    //整个文件的位置指针
            int    num2 = 0;

            try
            {
                a = Response.Headers["Content-Disposition"]; //attachment
                if (a != null)
                {
                    a = a.Substring(a.LastIndexOf("filename=") + 9);
                }
                long num1  = Length; //Response.ContentLength;
                bool flag1 = false;
                if (num1 == -1)
                {
                    flag1 = true;
                    num1  = 0x10000; //64k
                }
                byte[] buffer1 = new byte[(int)num1];
                int    p       = 0; //本块的位置指针
                string s       = Response.Headers["Content-Range"];
                if (s != null)
                {
                    s = s.Replace("bytes ", "");
                    s = s.Substring(0, s.IndexOf("-"));
                    P = Convert.ToInt32(s);
                }
                int    num3 = 0;
                Stream S    = Response.GetResponseStream();
                do
                {
                    num2  = S.Read(buffer1, num3, ((int)num1) - num3);
                    num3 += num2;
                    if (flag1 && (num3 == num1))
                    {
                        num1 += 0x10000;
                        byte[] buffer2 = new byte[(int)num1];
                        Buffer.BlockCopy(buffer1, 0, buffer2, 0, num3);
                        buffer1 = buffer2;
                    }

                    // lock (_SyncLockObject)
                    // {
                    // this._bytes += num2;
                    // }
                    if (num2 > 0)
                    {
                        if (this.DataReceive != null)
                        {
                            byte[] buffer = new byte[num2];
                            Buffer.BlockCopy(buffer1, p, buffer, 0, buffer.Length);
                            DownLoadState     dls  = new DownLoadState(RequestURL, Response.ResponseUri.AbsolutePath, FileName, a, P, num2, buffer);
                            DownLoadEventArgs dlea = new DownLoadEventArgs(dls);
                            //触发事件
                            this.OnDataReceive(dlea);
                            //System.Threading.Thread.Sleep(100);
                        }
                        p += num2; //本块的位置指针
                        P += num2; //整个文件的位置指针
                    }
                    else
                    {
                        break;
                    }
                }while (num2 != 0);

                S.Close();
                S = null;
                if (flag1)
                {
                    byte[] buffer3 = new byte[num3];
                    Buffer.BlockCopy(buffer1, 0, buffer3, 0, num3);
                    buffer1 = buffer3;
                }
                return(buffer1);
            }
            catch (Exception e)
            {
                var ea = ExceptionActions.Throw;
                if (this.ExceptionOccurrs != null)
                {
                    var x   = new DownLoadState(RequestURL, Response.ResponseUri.AbsolutePath, FileName, a, P, num2);
                    var eea = new ExceptionEventArgs(e, x);
                    ExceptionOccurrs(this, eea);
                    ea = eea.ExceptionAction;
                }

                if (ea == ExceptionActions.Throw)
                {
                    if (!(e is WebException) && !(e is SecurityException))
                    {
                        throw new WebException("net_webclient", e);
                    }
                    //throw;
                }
                return(null);
            }
        }