简体   繁体   English

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

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

I'm trying to read a RSS generated by wordpress with full text activated. 我正在尝试阅读由wordpress生成的RSS,其中包含全文本。 On firefox and IE9 an item data contains the element content:encoded : 在Firefox和IE9上,项目数据包含元素content:encoded

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

but when in a C# program I request the same rss url this node is not present. 但是在C#程序中我请求相同的rss url时,此节点不存在。 I do my C# request like this: 我这样执行我的C#请求:

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

Does I have to add an header to the request to have this specific field? 我是否必须在请求中添加标头才能具有此特定字段?

You don't need WebClient to download rss. 您不需要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");
}

EDIT 编辑

The problem is related with compression. 问题与压缩有关。 If the client doesn't support compression, then server doesn't send contents. 如果客户端不支持压缩,则服务器不会发送内容。

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();

I'm guessing Wordpress is choosing the "wrong" output format based on your Accept header. 我猜Wordpress正在根据您的Accept标头选择“错误的”输出格式。 Which feed is used is decided in /wp-content/feed.php : 使用哪个供稿由/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'
);

so instead of text/xml , try accepting application/rss+xml . 因此,请尝试接受application/rss+xml而不是text/xml application/rss+xml

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

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