如何检查链接是否是borken或关闭webrequest?

时间:2017-07-27 13:20:18

标签: c# httpwebrequest httpwebresponse

我从新闻网站的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;
    }

}

3 个答案:

答案 0 :(得分:1)

执行请求并获取HttpWebResponse时,您可以检查StatusCode属性,如果它是404或除200以外的任何内容,则可以将其视为不可用。

请参阅https://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.statuscode(v=vs.110).aspx

我建议创建一个类,以便在发生任何错误的情况下传递Web响应的状态代码

希望它有所帮助!

答案 1 :(得分:1)

您只需简单地集成到您的代码中,如下所示:

public static async Task<string> GetResponseStr(string link)
        {
            var final_response = string.Empty;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                StreamReader stream = new StreamReader(response.GetResponseStream());
                final_response = stream.ReadToEnd();
            }
            catch (Exception ex)
            {
                //DO whatever necessary like log or sending email to notify you   
            }

            return final_response;
        }

然后当你打电话时,在进一步处理之前添加一张支票:

    htmlStr = Utilities.GetResponseStr(haberYildizi.Link).Result;
    if (!string.IsNullOrEmpty(htmlStr))
    {

    }

答案 2 :(得分:0)

我只有端点网址和访问令牌。比我记录了那样的坏请求

   if (response.StatusCode == System.Net.HttpStatusCode.OK)
        {
            StreamReader stream = new StreamReader(response.GetResponseStream());
            string final_response = stream.ReadToEnd();
            return final_response;
            }
 else
            {
                Logger.CreateLogEntry("<== WebRequest ", "Could not Connect to server. Server Response Code: " + response.StatusCode);
                //Add bad request handler
                return null;
            }