Esempio 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;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Downloads the provided List of Feeds.
        /// You cannot call download while downloads are in progress.
        /// </summary>
        /// <param name="feeds">List of feeds to download.</param>
        public void Download(IList <Feed> feeds)
        {
            if (null != feeds && feeds.Count > 0)
            {
                _numOfRequests = feeds.Count;
                IsDownloading  = true;

                // Download each separate feed
                foreach (Feed feed in feeds)
                {
                    string feedURI = feed.FeedBaseURI;
                    if (null != feedURI)
                    {
                        try
                        {
                            HttpWebRequest feedRequest = HttpWebRequest.Create(feedURI) as HttpWebRequest;

                            // If the user has
                            if (App.ApplicationSettings.WifionlySetting)
                            {
                                try
                                {
                                    feedRequest.SetNetworkRequirement(NetworkSelectionCharacteristics.NonCellular);
                                }
                                catch (NetworkException ne)
                                {
                                    if (null != DownloadFailed)
                                    {
                                        DownloadFailed(this, new DownloadFailedEventArgs(ne)
                                        {
                                            WifiException = (ne.NetworkErrorCode == NetworkError.NetworkSelectionRequirementFailed)
                                        });
                                    }
                                    return;
                                }
                            }
                            if (null != feedRequest)
                            {
                                RequestState feedState = new RequestState()
                                {
                                    // Change the owner to the parent HTTPWebRequest.
                                    Request = feedRequest,
                                    // Change the argument to be the current feed to be downloaded.
                                    Argument = feed,
                                };

                                // Begin download.
                                feedRequest.BeginGetResponse(ResponseCallback, feedState);
                            }
                        }
                        catch (WebException we)
                        {
                            if (null != DownloadFailed)
                            {
                                DownloadFailed(this, new DownloadFailedEventArgs(we)
                                {
                                    BadConnectionException = true
                                });
                            }
                            return;
                        }
                    }
                }
            }
            else
            {
                throw new ArgumentNullException("No feeds");
            }
        }