Exemple #1
0
        private void ResponseDownloadCompleted(IAsyncResult result)
        {
            if (!mDisposedValue)
            {
                mActualDownload = null;
                AsyncDownloadArgs <T> asyncArgs = (AsyncDownloadArgs <T>)result.AsyncState;
                Stream          memStream       = null;
                WebException    dlException     = null;
                System.DateTime endTime         = System.DateTime.Now;
                int             size            = 0;

                try
                {
                    HttpWebResponse resp = (HttpWebResponse)asyncArgs.WR.EndGetResponse(result);

                    if (!asyncArgs.TimedOut)
                    {
                        try
                        {
                            if (asyncArgs.Settings.DownloadResponseStreamInternal)
                            {
                                memStream = MyHelper.CopyStream(resp.GetResponseStream());
                                endTime   = DateTime.Now;
                            }
                        }
                        catch (Exception ex)
                        {
                            dlException = this.GetOrCreateWebException(ex, resp);
                        }
                        finally
                        {
                            resp.Close();
                        }
                    }
                    else
                    {
                        dlException = new WebException("Timeout Exception.", null, GetTimeoutStatus(), resp);
                    }
                }
                catch (Exception ex)
                {
                    dlException = this.GetOrCreateWebException(ex, null);
                }
                finally
                {
                    if (memStream != null && memStream.CanSeek)
                    {
                        int.TryParse(memStream.Length.ToString(), out size);
                    }
                    mUserArgs = null;

                    StreamDownloadCompletedEventArgs <T> e = new StreamDownloadCompletedEventArgs <T>(asyncArgs.UserArgs, new DefaultResponse <Stream>(new ConnectionInfo(dlException, asyncArgs.Timeout, size, asyncArgs.StartTime, endTime), memStream), asyncArgs.Settings);
                    if (this.AsyncDownloadCompleted != null)
                    {
                        AsyncDownloadCompleted(this, e);
                    }
                }
            }
        }
 private void AsyncDownload_Completed(TimeoutWebClient <T> sender, StreamDownloadCompletedEventArgs <T> e)
 {
     using (TimeoutWebClient <T> snd = sender)
     {
         snd.AsyncDownloadCompleted -= this.AsyncDownload_Completed;
         if (mWebClients.Contains(sender))
         {
             mWebClients.Remove(sender);
         }
         using (System.IO.Stream stream = (e.Response.Result != null ? e.Response.Result : new System.IO.MemoryStream()))
         {
             ConnectionInfo conn = e.Response.Connection;
             T result            = default(T);
             try
             {
                 result = this.ConvertResult(e.Response.Connection, stream, e.UserSettings);
             }
             catch (Exception ex)
             {
                 conn = new ConnectionInfo(new ConversionException("An exception was thrown during result conversion process. See InnerException for more details.", ex), conn.Timeout, conn.SizeInBytes, conn.StartTime, conn.EndTime);
             }
             finally
             {
                 Response <T> resp = this.ConvertResponse(new DefaultResponse <T>(conn, result));
                 DownloadCompletedEventArgs <T> args = this.ConvertDownloadCompletedEventArgs(new DefaultDownloadCompletedEventArgs <T>(e.UserArgs, resp, e.UserSettings));
                 if (args != null && (AsyncDownloadCompleted != null || AsyncDownloadCompletedEvent != null))
                 {
                     AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(this);
                     if (AsyncDownloadCompleted != null)
                     {
                         asyncOp.Post(new SendOrPostCallback(delegate(object obj) { AsyncDownloadCompleted(this, (DownloadCompletedEventArgs <T>)obj); }), args);
                     }
                     if (AsyncDownloadCompletedEvent != null)
                     {
                         asyncOp.Post(new SendOrPostCallback(delegate(object obj) { AsyncDownloadCompletedEvent(this, (IDownloadCompletedEventArgs)obj); }), args);
                     }
                 }
             }
         }
     }
 }