【发布时间】:2017-07-27 13:20:17
【问题描述】:
我从新闻网站的 RSS 中获取数据并显示在列表视图中。(例如标题、描述、pubDate、标签、图像和评论计数)但是当链接断开或关闭时我没有什么问题,我出错了。 (在 mscorlib.dll 中发生了“System.AggregateException”类型的未处理异常) 我想检查链接是否损坏或关闭 webrequest 跳过此地址并继续到其他地址。
首先检查有关 webrequest 的主题,但我没有找到想要的答案。 只是这个主题给出了一个想法,但我没有集成到我的代码中。 (how to send web request to check the link whether the link is broken or not)
现在感谢您的帮助。
这是我的主要课程
public static Dictionary<string, HaberYildizi> HaberYildiziHaberList = new Dictionary<string, HaberYildizi>();
public static bool degismisMi = false;
public Dictionary<string, HaberYildizi> GetTagsHaberYildizi()
{
List<IBaseClass> yeniHaberList = new List<IBaseClass>();
degismisMi = false;
XmlDocument xdoc = new XmlDocument();
xdoc.Load("http://www.haberyildizi.com/rss.php");
XmlElement el = (XmlElement)xdoc.SelectSingleNode("/rss");
if (el != null)
el.ParentNode.RemoveChild(el);
XmlNode Haberler = el.SelectSingleNode("channel");
List<HaberYildizi> newHaberYildizi = new List<HaberYildizi>();
string htmlStr = String.Empty;
foreach (XmlNode haber in Haberler.SelectNodes("item"))
{
var link = haber.SelectSingleNode("link").InnerText;
if (HaberYildiziHaberList.ContainsKey(link))
continue;
HaberYildizi haberYildizi = new HaberYildizi();
haberYildizi.Title = WebUtility.HtmlDecode(haber.SelectSingleNode("title").InnerText);
if (haber.SelectSingleNode("description").InnerText.Contains("</a>"))
{
var str1 = haber.SelectSingleNode("description").InnerText.IndexOf("</a>");
var str2 = haber.SelectSingleNode("description").InnerText.Substring(str1 + 4);
haberYildizi.Description = WebUtility.HtmlDecode(str2);
}
else
{
haberYildizi.Description = WebUtility.HtmlDecode(haber.SelectSingleNode("description").InnerText);
}
haberYildizi.Image = haber.SelectSingleNode("image").InnerText;
haberYildizi.Link = link;
var format = DateTime.Parse(haber.SelectSingleNode("pubDate").InnerText.Replace("Z", ""));
haberYildizi.PubDate = format;
//*************************************
htmlStr = Utilities.GetResponseStr(haberYildizi.Link).Result; // mistake is here
//*************************************
haberYildizi.Tags = GetTags(htmlStr);
haberYildizi.Comment = GetCommentCount(htmlStr);
if (HaberYildiziHaberList.ContainsKey(haber.SelectSingleNode("link").InnerText) == false)
{
degismisMi = true;
HaberYildiziHaberList.Add(link, haberYildizi);
newHaberYildizi.Add(haberYildizi);
}
yeniHaberList.Add(haberYildizi);
}
return HaberYildiziHaberList;
}
public int GetCommentCount(string htmlStr)
{
int count = 0;
string commentKeyword = "label label-important\">";
var comment = htmlStr.IndexOf(commentKeyword) + 23;
if (comment != -1)
{
var comment2 = htmlStr.IndexOf("</span>", comment);
var comment4 = htmlStr.Substring(comment, comment2 - comment).Trim();
count = Convert.ToInt32(comment4);
}
return count;
}
public List<string> GetTags(string htmlStr)
{
List<string> listele = new List<string>();
string begenningKeyword = "<meta name=\"keywords\" content=\"";
var tags = htmlStr.IndexOf(begenningKeyword);
var final_response2 = htmlStr.Substring(tags + begenningKeyword.Length);
var tagsBol = final_response2.IndexOf("\" />");
var lastTags = final_response2.Substring(0, tagsBol);
var tagsSonuc = lastTags.Split(',');
foreach (var tag in tagsSonuc)
{
listele.Add(tag);
}
return listele;
}
}
这是我的实用程序类
public class Utilities
{
public static async Task<string> GetResponseStr(string link)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream());
string final_response = stream.ReadToEnd();
return final_response;
}
}
【问题讨论】:
-
“没有集成到我的代码”是什么意思?它是否显示任何异常或什么?
-
不不。我不知道如何放入我的代码中。
标签: c# httpwebrequest httpwebresponse