public static Feed Read(string uri, FeedReaderSettings settings=null) { Stream stream = null; if (settings == null) settings = new FeedReaderSettings(); try { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri); if (settings.HttpTimeout.HasValue) req.Timeout = settings.HttpTimeout.Value; // Some firewalls allow browser clients only. So let us be a browser ;) if(!String.IsNullOrEmpty(settings.HttpUserAgentString)) req.UserAgent = settings.HttpUserAgentString; // Add Accept string if (!String.IsNullOrEmpty(settings.HttpAcceptString)) req.Accept = settings.HttpAcceptString; // Add custom headers foreach (KeyValuePair<HttpRequestHeader, string> kvp in settings.HttpHeaders) { req.Headers.Add(kvp.Key, kvp.Value); } if (settings.FeedCredentials != null) req.Credentials = settings.FeedCredentials; WebResponse resp = req.GetResponse(); stream = resp.GetResponseStream(); return Read(stream, uri, settings); } catch (WebException ex) { errors.Add(ex); } return null; }
private bool readFeed(string uri) { FeedReaderSettings settings = new FeedReaderSettings(); settings.ReadModules = true; Feed feed = FeedReader.Read(uri, settings); if (FeedReader.HasErrors) { foreach (Exception ex in FeedReader.GetErrors()) { MessageBox.Show(ex.Message); } } if (feed != null) { showFeedInfo(feed); return true; } return false; }
public static Feed Read(Uri uri, FeedReaderSettings settings) { return Read(uri.ToString(), settings); }
/// <summary> /// If you have an open stream, uses it. The uri is not used except for modifying the feed /// </summary> /// <param name="stream"></param> /// <param name="uri"></param> /// <param name="settings"></param> /// <returns></returns> public static Feed Read(Stream stream, String uri, FeedReaderSettings settings) { XmlReader xmlReader = XmlReader.Create(stream, new XmlReaderSettings() { DtdProcessing = DtdProcessing.Ignore, XmlResolver = new XmlCachingResolver(true) }); Parser parser = null; try{ Feed feed = initialize(xmlReader); if (feed != null) { xmlReader.MoveToContent(); switch (feed.Version) { case FeedVersion.Atom03: case FeedVersion.Atom10: parser = new AtomParser(xmlReader.ReadSubtree(), feed); parser.ReadModules = settings.ReadModules; parser.Parse(); break; case FeedVersion.RSS090: case FeedVersion.RSS10: parser = new RssOldParser(xmlReader.ReadSubtree(), feed); parser.ReadModules = settings.ReadModules; parser.Parse(); break; case FeedVersion.RSS091: case FeedVersion.RSS092: case FeedVersion.RSS20: XmlReader subReader20 = xmlReader.ReadSubtree(); while (subReader20.Read()) if (subReader20.NodeType == XmlNodeType.Element && subReader20.Name == "channel") break; parser = new RssParser(subReader20.ReadSubtree(), feed); parser.ReadModules = settings.ReadModules; parser.Parse(); break; } if (feed.XmlUri == null) feed.XmlUri = new FeedUri(uri); if (feed.WebUri == null) feed.WebUri = new FeedUri(String.Empty); } return feed; } catch (Exception ex) { errors.Add(ex); string debugExceptionMsg = string.Empty; if (null != parser && !String.IsNullOrEmpty(parser.CurrentNodeBeingParsed)) debugExceptionMsg += "Node being parsed was: " + parser.CurrentNodeBeingParsed+" "; if (null != parser && null != parser.CurrentFeedItemBeingParsed) debugExceptionMsg += "Most recent item: " + parser.CurrentFeedItemBeingParsed.ToString() + " "; if(!String.IsNullOrEmpty(debugExceptionMsg)) errors.Add(new Exception(debugExceptionMsg)); } finally { if (xmlReader != null) xmlReader.Close(); if (stream != null) stream.Close(); } return null; }