繁体   English   中英

使用C#读取wordpress RSS-内容不同

[英]Reading wordpress RSS with C# - Content different

我正在尝试阅读由wordpress生成的RSS,其中包含全文本。 在Firefox和IE9上,项目数据包含元素content:encoded

<content:encoded><![CDATA[bla bla bla]]></content:encoded>            

但是在C#程序中我请求相同的rss url时,此节点不存在。 我这样执行我的C#请求:

   WebClient client = new WebClient();
   client.Encoding = Encoding.UTF8;
   client.Headers.Add("Accept", "application/xml");
   var xml = client.DownloadString(url)

我是否必须在请求中添加标头才能具有此特定字段?

您不需要WebClient来下载rss。

XDocument wp = XDocument.Load("http://wordpress.org/news/feed/");
XNamespace ns = XNamespace.Get("http://purl.org/rss/1.0/modules/content/");

foreach (var content in wp.Descendants(ns + "encoded"))
{
    Console.WriteLine(System.Net.WebUtility.HtmlDecode(content.Value)+"\n\n");
}

编辑

问题与压缩有关。 如果客户端不支持压缩,则服务器不会发送内容。

WebClient web = new WebClient();
web.Headers["Accept-Encoding"] = "gzip,deflate,sdch";

var zip = new System.IO.Compression.GZipStream(
    web.OpenRead("http://www.whiskymag.fr/feed/?post_type=sortir"), 
    System.IO.Compression.CompressionMode.Decompress);

string rss = new StreamReader(zip, Encoding.UTF8).ReadToEnd();

我猜Wordpress正在根据您的Accept标头选择“错误的”输出格式。 使用哪个供稿由/wp-content/feed.php决定:

$types = array(
    'rss'  => 'application/rss+xml',
    'rss2' => 'application/rss+xml',
    'rss-http'  => 'text/xml',
    'atom' => 'application/atom+xml',
    'rdf'  => 'application/rdf+xml'
);

因此,请尝试接受application/rss+xml而不是text/xml application/rss+xml

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM