Beispiel #1
0
        private static RssFeed read(string url, HttpWebRequest request, RssFeed oldFeed)
        {
            // ***** Marked for substantial improvement
            RssFeed    feed    = new RssFeed();
            RssElement element = null;
            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)
                {
                    if (oldFeed != null)
                    {
                        oldFeed.cached = true;
                        return(oldFeed);
                    }

                    throw;
                }
                break;
            }

            if (stream != null)
            {
                RssReader reader = null;
                try
                {
                    reader = new RssReader(stream);
                    do
                    {
                        element = reader.Read();
                        if (element is RssChannel)
                        {
                            feed.Channels.Add((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);
        }
        private static RssFeed read(string url, HttpWebRequest request, RssFeed oldFeed)
        {
            // ***** Marked for substantial improvement
            RssFeed feed = new RssFeed();
            RssElement element = null;
            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":
                    goto case "http";
                case "http":
                    if (request == null)
                        request = (HttpWebRequest)WebRequest.Create(uri);
                    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 != "")
                                feed.encoding = Encoding.GetEncoding(response.ContentEncoding);
                        }
                        catch {}
                        stream = response.GetResponseStream();
                    }
                    catch (WebException we)
                    {
                        if (oldFeed != null)
                        {
                            oldFeed.cached = true;
                            return oldFeed;
                        }
                        else throw we; // bad
                    }
                    break;
            }

            if (stream != null)
            {
                RssReader reader = null;
                try
                {
                    reader = new RssReader(stream);
                    do
                    {
                        element = reader.Read();
                        if (element is RssChannel)
                            feed.Channels.Add((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;
        }