Example #1
0
 private void OnDataReceive(DownLoadEventArgs e)
 {
     //触发数据到达事件
     DataReceive(this, e);
 }
Example #2
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)
            {
                ExceptionActions ea = ExceptionActions.Throw;
                if (this.ExceptionOccurrs != null)
                {
                    DownLoadState      x   = new DownLoadState(RequestURL, Response.ResponseUri.AbsolutePath, FileName, a, P, num2);
                    ExceptionEventArgs 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);
            }
        }