public class CommentRss : IHttpHandler { public void ProcessRequest(HttpContext context) { string url = context.Request.QueryString["url"]; WebClient webClient = new WebClient(); webClient.Encoding = Encoding.UTF8; string html = webClient.DownloadString(url); context.Response.ContentType = "text/xml"; context.Response.ContentEncoding = Encoding.UTF8; SyndicationFeed feed = GetRssFeed(url, html); Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed); XmlWriter rssWriter = XmlWriter.Create(context.Response.Output); rssFormatter.WriteTo(rssWriter); rssWriter.Close(); } private static SyndicationFeed GetRssFeed(string url, string html) { string titlePattern = @"<head id=""Head""><title>([/s/S]*?)</title>"; string commentBlockPattern = @"<div id=""AjaxHolder_UpdatePanel1"">([/s/S]*?)</div>[^<]*<script language=""javascript"">"; string commentPattern = @"<div class=""posthead"">/s*<h2>/s*<a title=""permalink: (?<title>[^""]+)"" href=""(?<url>[^""]+)"">[/s/S]*?</h2>/s+(?<time>/d/d/d/d-/d/d-/d/d /d/d:/d/d) by <a[^>]*>(?<user>[^<]*)</a>[/s/S]*?<div class=""postbody""><span[^>]*>(?<content>[/s/S]*?)</span></div>/s*</div>/s*<div class=""post"">"; string title = HttpUtility.HtmlDecode(Regex.Match(html, titlePattern, RegexOptions.IgnoreCase).Groups[1].Value).Trim(); Match blockMatch = Regex.Match(html, commentBlockPattern, RegexOptions.IgnoreCase); string blockHtml = blockMatch.Groups[1].Value + "/n/t/n<div class=/"post/">"; MatchCollection commentMatches = Regex.Matches(blockHtml, commentPattern, RegexOptions.IgnoreCase); var itemList = commentMatches.Cast<Match>().Select(m => new { Title = HttpUtility.HtmlDecode(m.Groups["title"].Value.Trim()), Url = "http://www.cnblogs.com" + m.Groups["url"].Value.Trim(), Time = DateTime.ParseExact(m.Groups["time"].Value.Trim(), "yyyy-MM-dd HH:mm", null), User = HttpUtility.HtmlDecode(m.Groups["user"].Value.Trim()), Content = HttpUtility.HtmlDecode(m.Groups["content"].Value.Trim()) }).ToList(); SyndicationFeed feed = new SyndicationFeed(title, "Comment RSS", new Uri(url), "FeedID", DateTime.Now); List<SyndicationItem> items = new List<SyndicationItem>(); itemList.ForEach(item => { var hashIndex = item.Url.LastIndexOf("#"); var id = item.Url.Substring(hashIndex); var rssItem = new SyndicationItem(item.Title, item.Content, new Uri(item.Url), id, item.Time); rssItem.Authors.Add(new SyndicationPerson(item.User, "", "")); items.Add(rssItem); }); feed.Items = items; return feed; } public bool IsReusable { get { return false; } } }