Esempio n. 1
0
            private void ReadNetByte()
            {
                FileStream ttempfile = null;

                try
                {
                    long thaveindex = 0;
                    if (File.Exists(TempFile))
                    {
                        if (!mIsClear)
                        {
                            ttempfile  = System.IO.File.OpenWrite(TempFile);
                            thaveindex = ttempfile.Length;
                            ttempfile.Seek(thaveindex, SeekOrigin.Current);
                        }
                        else
                        {
                            File.Delete(TempFile);
                            thaveindex = 0;
                        }
                    }

                    mReqest         = (HttpWebRequest)HttpWebRequest.Create(SourceURL);
                    mReqest.Timeout = 5000;

                    if (SourceURL.Contains("https://"))
                    {
                        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    }

                    if (thaveindex > 0)
                    {
                        mReqest.AddRange((int)thaveindex);
                    }

                    int tindex = 0;
                    while (tindex++ < 4 && mThreadRuning)
                    {
                        try
                        {
                            mResponse = mReqest.GetResponse();
                            break;
                        }
                        catch (System.Exception _error)
                        {
                            DLog.LogFormat("ReadNetByte 获取Response失败,尝试次数{0},error = {1}", tindex, _error);
                        }
                    }

                    if (mResponse == null)
                    {
                        throw new System.NullReferenceException("ReadNetByte 获取Response失败.");
                    }

                    mHttpStream   = mResponse.GetResponseStream();
                    ContentLength = mResponse.ContentLength;

                    if (ttempfile == null)
                    {
                        ttempfile = System.IO.File.Create(TempFile);
                    }

                    int    tcount    = 0;
                    int    tlen      = 1024;
                    byte[] tbuffer   = new byte[tlen];
                    int    tReadSize = 0;
                    tReadSize = mHttpStream.Read(tbuffer, 0, tlen);
                    while (tReadSize > 0 && mThreadRuning)
                    {
                        DownLoadedLength += tReadSize;
                        ttempfile.Write(tbuffer, 0, tReadSize);
                        tReadSize = mHttpStream.Read(tbuffer, 0, tlen);

                        if (++tcount >= 512)
                        {
                            ttempfile.Flush();
                            tcount = 0;
                        }
                    }
                }
                catch (System.Exception _error)
                {
                    Error = _error.ToString();
                }

                if (ttempfile != null)
                {
                    ttempfile.Close();
                }

                if (DownLoadedLength == ContentLength)
                {
                    if (File.Exists(TempFile))
                    {
                        if (File.Exists(CompleteFile))
                        {
                            File.Delete(CompleteFile);
                        }
                        File.Move(TempFile, CompleteFile);
                    }
                }
                else
                {
                    if (Error == null)
                    {
                        Error = "文件未能完成下载.Stream 被中断.";
                    }
                }

                CloseHttpClient();
                mIsDone = true;
            }
Esempio n. 2
0
        private void ReadNetByte()
        {
            Error = string.Format("[URL] = {0},[Error] = Can not completed.Stream erro.", SourceURL);
            long       tdownloadSize     = 0;
            long       tneedDownLoadSize = 0;
            FileStream ttempfile         = null;

            try
            {
                if (!Directory.Exists(TempPath))
                {
                    Directory.CreateDirectory(TempPath);
                }
                if (!Directory.Exists(DestinationPath))
                {
                    Directory.CreateDirectory(DestinationPath);
                }

                long thaveindex      = 0;
                bool isFirst         = !File.Exists(TempFile);
                bool isClearDownload = isFirst || mIsClear;
                if (isClearDownload)
                {
                    if (!isFirst)
                    {
                        File.Delete(TempFile);
                    }
                    thaveindex = 0;
                }
                else
                {
                    if (!isFirst)
                    {
                        ttempfile  = System.IO.File.OpenWrite(TempFile);
                        thaveindex = ttempfile.Length;
                        ttempfile.Seek(thaveindex, SeekOrigin.Current);
                    }
                }

                mReqest         = (HttpWebRequest)HttpWebRequest.Create(SourceURL);
                mReqest.Timeout = 20000;

                if (SourceURL.Contains("https://"))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                }

                if (thaveindex > 0)
                {
                    mReqest.AddRange((int)thaveindex);
                }

                string rspError = null;
                try
                {
                    mResponse = mReqest.GetResponse();
                }
                catch (System.Exception _error)
                {
                    rspError = _error.Message;
                }

                if (mResponse == null || rspError != null)
                {
                    string terro = string.Format("ReadNetByte Get Response fail.[Error] = {0}", rspError);
                    throw new System.NullReferenceException(terro);
                }

                mHttpStream       = mResponse.GetResponseStream();
                tneedDownLoadSize = mResponse.ContentLength;

                DownLoadedLength = thaveindex;
                if (isClearDownload)
                {
                    ContentLength = tneedDownLoadSize;
                }

                if (ttempfile == null)
                {
                    ttempfile = System.IO.File.Create(TempFile);
                }


                int    tcount    = 0;
                int    tlen      = 1024;
                byte[] tbuffer   = new byte[tlen];
                int    tReadSize = 0;
                tReadSize = mHttpStream.Read(tbuffer, 0, tlen);
                while (tReadSize > 0 && mThreadRuning)
                {
                    DownLoadedLength += tReadSize;
                    tdownloadSize    += tReadSize;

                    ttempfile.Write(tbuffer, 0, tReadSize);
                    tReadSize = mHttpStream.Read(tbuffer, 0, tlen);

                    if (++tcount >= 512)
                    {
                        ttempfile.Flush();
                        tcount = 0;
                    }
                }
            }
            catch (System.Exception _error)
            {
                Error = string.Format("[URL] = {0},[Error] = {1}", SourceURL, _error.Message);
            }

            if (ttempfile != null)
            {
                ttempfile.Close();
            }

            try
            {
                if (tdownloadSize == tneedDownLoadSize)
                {
                    DownLoadComplete();
                }
                else
                {
                    if (string.IsNullOrEmpty(Error))
                    {
                        Error = string.Format("[URL] = {0},[Error] = Can not completed.Stream erro.", SourceURL);
                    }
                }
            }
            catch (System.Exception erro)
            {
                Error = string.Format("[URL] = {0},[Error] = {1}", SourceURL, erro.Message);
            }

            FinishedThread();
        }