Пример #1
0
 /// <summary>
 ///     Reads the specified RSS feed
 /// </summary>
 /// <param name="Request"> The specified way to connect to the web server </param>
 /// <param name="oldFeed"> The cached version of the feed </param>
 /// <returns> The current contents of the feed </returns>
 /// <remarks>
 ///     Will not download the feed if it has not been modified
 /// </remarks>
 public static RssFeed Read(HttpWebRequest Request, RssFeed oldFeed)
 {
     return read(oldFeed.url, Request, oldFeed);
 }
Пример #2
0
        private static RssFeed read(string url, HttpWebRequest request, RssFeed oldFeed)
        {
            // ***** Marked for substantial improvement
            RssFeed feed = new RssFeed();
            Stream stream = null;
            Uri uri = new Uri(url);
            feed.url = url;

            switch (uri.Scheme)
            {
                case "file":
                    feed.lastModified = File.GetLastWriteTime(url);
                    if ((oldFeed != null) && (feed.LastModified == oldFeed.LastModified))
                    {
                        oldFeed.cached = true;
                        return oldFeed;
                    }
                    stream = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    break;

                case "https":
                case "http":
                    if (request == null)
                    {
                        request = (HttpWebRequest) WebRequest.Create(uri);
                        request.Proxy = WebProxy.GetDefaultProxy();
                        request.Proxy.Credentials = CredentialCache.DefaultCredentials;
                    }

                    if (oldFeed != null)
                    {
                        request.IfModifiedSince = oldFeed.LastModified;
                        request.Headers.Add("If-None-Match", oldFeed.ETag);
                    }

                    try
                    {
                        HttpWebResponse response = (HttpWebResponse) request.GetResponse();
                        feed.lastModified = response.LastModified;
                        feed.etag = response.Headers["ETag"];
                        try
                        {
                            if (response.ContentEncoding != String.Empty)
                                feed.encoding = Encoding.GetEncoding(response.ContentEncoding);
                        }
                        catch
                        {
                        }

                        stream = response.GetResponseStream();
                    }
                    catch (WebException we)
                    {
                        if (oldFeed != null)
                        {
                            oldFeed.cached = true;
                            return oldFeed;
                        }
                        throw we; // bad
                    }
                    break;
            }

            if (stream != null)
            {
                RssReader reader = null;
                try
                {
                    reader = new RssReader(stream);
                    RssElement element = null;
                    do
                    {
                        element = reader.Read();
                        if (element is RssChannel.RssChannel)
                            feed.Channels.Add((RssChannel.RssChannel) element);
                    } while (element != null);
                    feed.rssVersion = reader.Version;
                }
                finally
                {
                    feed.exceptions = reader.Exceptions;
                    reader.Close();
                }
            }
            else
                throw new ApplicationException("Not a valid Url");

            return feed;
        }
Пример #3
0
 /// <summary>
 ///     Reads the specified RSS feed
 /// </summary>
 /// <param name="oldFeed"> The cached version of the feed </param>
 /// <returns> The current contents of the feed </returns>
 /// <remarks>
 ///     Will not download the feed if it has not been modified
 /// </remarks>
 public static RssFeed Read(RssFeed oldFeed)
 {
     return read(oldFeed.url, null, oldFeed);
 }