private static void ReadCallback(IAsyncResult ar)
        {
            DotNetUrlDownloader.DownloadState downloadState = (DotNetUrlDownloader.DownloadState)ar.get_AsyncState();
            bool flag = true;

            try
            {
                Stream responseStream = downloadState.responseStream;
                int    num            = responseStream.EndRead(ar);
                if (num > 0)
                {
                    flag = false;
                    Array.Copy(downloadState.readBuffer, 0, downloadState.downloadResult.downloadData, downloadState.readPos, num);
                    downloadState.readPos += num;
                    responseStream.BeginRead(downloadState.readBuffer, 0, downloadState.readBuffer.Length, new AsyncCallback(DotNetUrlDownloader.ReadCallback), downloadState);
                }
                else if (num == 0)
                {
                    downloadState.downloadResult.succeeded = true;
                }
            }
            catch (Exception)
            {
            }
            if (flag)
            {
                DotNetUrlDownloader.FinishDownload(downloadState);
            }
        }
        private static void FinishDownload(DotNetUrlDownloader.DownloadState state)
        {
            if (!state.downloadResult.succeeded && state.numRetriesLeft > 0)
            {
                state.numRetriesLeft--;
                DotNetUrlDownloader.Download(state);
                return;
            }
            List <DotNetUrlDownloader.DownloadResult> completedDownloads = state.downloader.m_completedDownloads;

            lock (completedDownloads)
            {
                state.downloader.m_completedDownloads.Add(state.downloadResult);
            }
        }
        public void Download(string url, UrlDownloadCompletedCallback cb, UrlDownloaderConfig config)
        {
            WebRequest request = WebRequest.Create(url);

            DotNetUrlDownloader.DownloadResult downloadResult = new DotNetUrlDownloader.DownloadResult();
            downloadResult.callback = cb;
            DotNetUrlDownloader.Download(new DotNetUrlDownloader.DownloadState
            {
                downloader     = this,
                host           = url,
                downloadResult = downloadResult,
                request        = request,
                numRetriesLeft = config.numRetries,
                timeoutMs      = config.timeoutMs
            });
        }
 private static void ResponseCallback(IAsyncResult ar)
 {
     DotNetUrlDownloader.DownloadState asyncState = (DotNetUrlDownloader.DownloadState)ar.AsyncState;
     try
     {
         WebResponse webResponse    = asyncState.request.EndGetResponse(ar);
         Stream      responseStream = webResponse.GetResponseStream();
         asyncState.responseStream = responseStream;
         asyncState.downloadResult.downloadData = new byte[checked ((IntPtr)webResponse.ContentLength)];
         responseStream.BeginRead(asyncState.readBuffer, 0, (int)asyncState.readBuffer.Length, new AsyncCallback(DotNetUrlDownloader.ReadCallback), asyncState);
     }
     catch (Exception exception)
     {
         DotNetUrlDownloader.FinishDownload(asyncState);
     }
 }
 private static void ResponseCallback(IAsyncResult ar)
 {
     DotNetUrlDownloader.DownloadState downloadState = (DotNetUrlDownloader.DownloadState)ar.get_AsyncState();
     try
     {
         WebRequest  request        = downloadState.request;
         WebResponse webResponse    = request.EndGetResponse(ar);
         Stream      responseStream = webResponse.GetResponseStream();
         downloadState.responseStream = responseStream;
         downloadState.downloadResult.downloadData = new byte[webResponse.get_ContentLength()];
         responseStream.BeginRead(downloadState.readBuffer, 0, downloadState.readBuffer.Length, new AsyncCallback(DotNetUrlDownloader.ReadCallback), downloadState);
     }
     catch (Exception)
     {
         DotNetUrlDownloader.FinishDownload(downloadState);
     }
 }
 private static void Download(DotNetUrlDownloader.DownloadState state)
 {
     try
     {
         IAsyncResult asyncResult = state.request.BeginGetResponse(new AsyncCallback(DotNetUrlDownloader.ResponseCallback), state);
         int          num         = state.timeoutMs;
         if (num < 0)
         {
             num = -1;
         }
         state.timeoutWatchHandle = asyncResult.get_AsyncWaitHandle();
         state.timeoutWaitHandle  = ThreadPool.RegisterWaitForSingleObject(state.timeoutWatchHandle, new WaitOrTimerCallback(DotNetUrlDownloader.TimeoutCallback), state, num, true);
     }
     catch (Exception)
     {
         DotNetUrlDownloader.FinishDownload(state);
     }
 }