Reading wordpress RSS with C# - Content different

2019-08-23 23:58发布

I'm trying to read a RSS generated by wordpress with full text activated. On firefox and IE9 an item data contains the element 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. I do my C# request like this:

   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?

标签: c# wordpress rss
2条回答
Viruses.
2楼-- · 2019-08-24 00:41

I'm guessing Wordpress is choosing the "wrong" output format based on your Accept header. Which feed is used is decided in /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.

查看更多
欢心
3楼-- · 2019-08-24 00:46

You don't need WebClient to download 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();
查看更多
登录 后发表回答