【问题标题】:How to check if link is borken or closed with webrequest?如何使用 webrequest 检查链接是否损坏或关闭?
【发布时间】: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


【解决方案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】:

    在执行请求并获取 HttpWebResponse 时,您可以检查 StatusCode 属性,如果它是 404 或除 200 之外的任何值,则可以认为它不可用。

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

    我建议创建一个类来传递 Web 响应的状态代码,以防万一出现任何问题

    希望对你有帮助!

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-25
        • 2013-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-26
        • 2012-04-13
        • 2011-04-21
        相关资源
        最近更新 更多