Exemplo n.º 1
0
        private async void Fetch(string url, OnFeedFetchedHandler onFeedFetched, bool externalCall)
        {
            if (externalCall && _busy)
            {
                return;
            }
            _busy = true;

            _url = url;

            _canceled      = false;
            _onFeedFetched = onFeedFetched;

            var client = new System.Net.Http.HttpClient(new System.Net.Http.HttpClientHandler()
            {
                AllowAutoRedirect = true
            });

            try
            {
                var content = await client.GetStringAsync(_url);

                if (_canceled)
                {
                    return;
                }

                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(content);

                    if (_onFeedFetched != null)
                    {
                        _onFeedFetched(FeedRefreshResult.Success, doc, null, _url);
                        return;
                    }
                }
                catch (XmlException)
                {
                    // maybe its non-xml html with a link to a feed..

                    try
                    {
                        MatchCollection matches = _altLinkRgx.Matches(content);
                        foreach (Match match in matches)
                        {
                            string linkContent = match.Groups[1].Value;
                            if (linkContent.Contains("type=\"application/rss+xml\""))
                            {
                                Match urlMatch = _altLinkURLRgx.Match(linkContent);
                                if (urlMatch.Success)
                                {
                                    if (_onFeedFetched != null)
                                    {
                                        string redirectUrl = urlMatch.Groups[1].Value;

                                        Fetch(redirectUrl, _onFeedFetched, false);

                                        return;
                                    }
                                }
                            }
                        }

                        if (_onFeedFetched != null)
                        {
                            _onFeedFetched(FeedRefreshResult.InvalidData, null, "Cannot connect to podcast, the resource does not appear to contain a podcast.", null);
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (_onFeedFetched != null)
                        {
                            _onFeedFetched(FeedRefreshResult.UnableToConnect, null, "Cannot connect to podcast, " + ex.Message, null);
                            return;
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Message == "The request was aborted: The request was canceled.")
                {
                    if (_onFeedFetched != null)
                    {
                        _onFeedFetched(FeedRefreshResult.Canceled, null, null, null);
                        return;
                    }
                }
                else
                {
                    if (_onFeedFetched != null)
                    {
                        _onFeedFetched(FeedRefreshResult.UnableToConnect, null, ex.Message, null);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                if (_onFeedFetched != null)
                {
                    _onFeedFetched(FeedRefreshResult.UnableToConnect, null, ex.Message, null);
                    return;
                }
            }
            finally
            {
                _busy = false;
            }
        }
Exemplo n.º 2
0
 public void FetchIfNotBusy(string url, OnFeedFetchedHandler onFeedFetched)
 {
     Fetch(url, onFeedFetched, true);
 }