使用 C# 阅读 wordpress RSS - 内容不同 [英] Reading wordpress RSS with C# - Content different

查看:35
本文介绍了使用 C# 阅读 wordpress RSS - 内容不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试阅读 wordpress 生成的已激活全文的 RSS.在 Firefox 和 IE9 上,项目数据包含元素 content:encoded:

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>            

但是当我在 C# 程序中请求相同的 rss url 时,该节点不存在.我做我的 C# 请求是这样的:

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?

推荐答案

您不需要 WebClient 来下载 rss.

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");
}

编辑

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

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

这篇关于使用 C# 阅读 wordpress RSS - 内容不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆