拉RSS订阅来自Facebook页面 [英] Pull RSS Feeds From Facebook Page
问题描述
我需要帮助拉RSS提要,从一个Facebook页面,我用下面的代码,但它不断给我一个错误:
I need help to pull RSS feeds from a facebook page I'm using the following code but it keeps giving me an error :
string url =
"https://www.facebook.com/feeds/page.php?id=40796308305&format=rss20";
XmlReaderSettings settings =
new XmlReaderSettings
{
XmlResolver = null,
DtdProcessing=DtdProcessing.Parse,
};
XmlReader reader = XmlReader.Create(url,settings);
SyndicationFeed feed = SyndicationFeed.Load(reader);
foreach (var item in feed.Items)
{
Console.WriteLine(item.Id);
Console.WriteLine(item.Title.Text);
Console.WriteLine(item.Summary.Text);
}
if (reader != null) reader.Close();
此代码工作完美地与任何博客或网页RSS,但与Facebook RSS它给人以下列异常消息
This code works perfectly with any blog or page rss but with Facebook rss it give an exception with the following message
一个名为HTML和命名空间http://www.w3.org/1999/xhtml的元素不是允许饲料格式。
The element with name 'html' and namespace 'http://www.w3.org/1999/xhtml' is not an allowed feed format.
感谢
推荐答案
Facebook将在这种情况下返回HTML,因为它不喜欢被XmlReader中提供的用户代理。既然你不能自定义它,你需要一个不同的解决方案,以抢进。这应该解决您的问题:
Facebook will return HTML in this instance because it doesn't like the User Agent supplied by XmlReader. Since you can't customize it, you will need a different solution to grab the feed. This should solve your problem:
var req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.UserAgent = "Fiddler";
var rep = req.GetResponse();
var reader = XmlReader.Create(rep.GetResponseStream());
SyndicationFeed feed = SyndicationFeed.Load(reader);
这是严格的Facebook的行为,但建议的变更应该同样适合那些其他网站还好你当前的实现。
This is strictly a behavior of Facebook, but the proposed change should work equally well for other sites that are okay with your current implementation.
这篇关于拉RSS订阅来自Facebook页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!