Ejemplo n.º 1
0
        /// <summary>
        /// Callback method called when the "Download" method returns from an HTTPWebRequest.
        /// </summary>
        /// <param name="result">The result of the asynchronous operation.</param>
        private void ResponseCallback(IAsyncResult result)
        {
            RequestState state = (RequestState)result.AsyncState;

            try
            {
                HttpWebRequest request = state.Request as HttpWebRequest;
                if (null != request)
                {
                    // Retrieve response.
                    using (HttpWebResponse response = request.EndGetResponse(result) as HttpWebResponse)
                    {
                        if (null != response && response.StatusCode == HttpStatusCode.OK)
                        {
                            using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                            {
                                Feed parentFeed = state.Argument as Feed;

                                if (null != parentFeed)
                                {
                                    // Collection to store all articles
                                    Collection <Article> parsedArticles = _parser.ParseItems(reader, parentFeed);

                                    // Raise event for a single feed downloaded.
                                    if (null != SingleDownloadFinished)
                                    {
                                        SingleDownloadFinished(this, new SingleDownloadFinishedEventArgs(parentFeed, parsedArticles));
                                    }

                                    // Add to all downloads dictionary and raise AllDownloadsFinished if all async requests have finished.
                                    Downloads.Add(parentFeed, parsedArticles);

                                    lock (_lockObject)
                                    {
                                        IsDownloading = ((--_numOfRequests) > 0);
                                    }
                                    if (_numOfRequests <= 0 && null != AllDownloadsFinished)
                                    {
                                        AllDownloadsFinished(this, new AllDownloadsFinishedEventArgs(Downloads));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (WebException we)
            {
                if (null != DownloadFailed)
                {
                    DownloadFailed(this, new DownloadFailedEventArgs(we)
                    {
                        BadConnectionException = true
                    });
                }
                return;
            }
            catch (XmlException e)
            {
                if (null != DownloadFailed)
                {
                    DownloadFailed(this, new DownloadFailedEventArgs(e));
                }
                return;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Callback method called when the "Download" method returns from an HTTPWebRequest.
        /// </summary>
        /// <param name="result">The result of the asynchronous operation.</param>
        private void ResponseCallback(IAsyncResult result)
        {
            RequestState state      = result.AsyncState as RequestState;
            Feed         parentFeed = state.Argument as Feed;

            if (null != parentFeed)
            {
                HttpWebRequest request = state.Request as HttpWebRequest;

                // Progress only if this download has not been timed out.

                if (null != request)
                {
                    // Retrieve response.
                    try
                    {
                        using (HttpWebResponse response = request.EndGetResponse(result) as HttpWebResponse)
                        {
                            if (null != response && response.StatusCode == HttpStatusCode.OK)
                            {
                                using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                                {
                                    // Collection to store all articles
                                    Collection <Article> parsedArticles = _parser.ParseItems(reader, parentFeed);

                                    // Raise event for a single feed downloaded.
                                    if (null != SingleDownloadFinished)
                                    {
                                        SingleDownloadFinished(this, new SingleDownloadFinishedEventArgs(parentFeed, parsedArticles));
                                    }

                                    // Add to all downloads dictionary and raise AllDownloadsFinished if all async requests have finished.
                                    Downloads.Add(parentFeed, parsedArticles);
                                    CheckIfDone();
                                }
                            }
                        }
                    }
                    catch (WebException we)
                    {
                        if (we.Status == WebExceptionStatus.RequestCanceled)
                        {
                            // The web request was timed out. Let debug know it failed.
                            System.Diagnostics.Debug.WriteLine("ERROR: Feed \"" + parentFeed.FeedTitle + "\" was timed out.");
                            CheckIfDone();
                            return;
                        }
                        else if (we.Message == "The remote server returned an error: NotFound.")
                        {
                            // The web site did not respond. This means one of two things: Either the web site is bad, or you have no internet.
                            // If error code NotFound is received, then there is no connection. Throw the exception.
                            if ((we.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
                            {
                                // WebTools throws an exception if without connection. Warn the user.
                                CheckIfDone();
                                if (!IsDownloading)
                                {
                                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                                    {
                                        MessageBox.Show("FeedCast is unable to reach a connection. Please check your network connectivity.");
                                    });
                                }
                            }
                            // If error code InternalServerError is received, then it is a faulty site.
                            else
                            {
                                System.Diagnostics.Debug.WriteLine("ERROR: Feed \"" + parentFeed.FeedTitle + "\" is faulty.");
                                CheckIfDone();
                                return;
                            }
                        }
                        else
                        {
                            // Unexpected web exception.
                            throw we;
                        }
                    }
                }
            }
        }