简体   繁体   English

如何检查它是否是404错误页面并使用HtmlAgilityPack做一些事情

[英]How to check if it is 404 error page and do something using HtmlAgilityPack

I'm trying to get multiple data from different urls using HtmlAgilityPack. 我正在尝试使用HtmlAgilityPack从不同的URL获取多个数据。

  • It will get product prices. 它将获得产品价格。
  • But when product stock is 0. They are closing the page. 但是当产品库存为0时,它们将关闭页面。

My program adding prices to listbox. 我的程序将价格添加到列表框。 When page giving 404 It should add empty listbox item. 当页面给出404时,应添加一个空的列表框项。

Is there any way to make program simpler? 有什么方法可以使程序更简单? I can't use same Variables at the same button. 我不能在同一按钮上使用相同的变量。 I'm adding same code changing the numbers (6). 我要添加相同的代码来更改数字(6)。

WebRequest SiteyeBaglantiTalebi06 = HttpWebRequest.Create("https://www.themia.com.tr/The-Mia-Dekor-Mermer-22-Cm-Gri,PR-2432.html");
WebResponse GelenCevap06 = SiteyeBaglantiTalebi06.GetResponse();
StreamReader CevapOku06 = new StreamReader(GelenCevap06.GetResponseStream());

string KaynakKodlar06 = CevapOku06.ReadToEnd();
int IcerikBaslangicIndex06 = KaynakKodlar06.IndexOf("<div class=\"productPrice\">") + 122;
int IcerikBitisIndex06 = KaynakKodlar06.Substring(IcerikBaslangicIndex06).IndexOf("</div>");

listBox3.Items.Add((KaynakKodlar06.Substring(IcerikBaslangicIndex06, IcerikBitisIndex06)));

If you cast the WebResponse you got to an HttpWeResponse you can access the StatusCode property - https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.statuscode?view=netframework-4.7.2#System_Net_HttpWebResponse_StatusCode ; 如果你投的WebResponse你有一个HttpWeResponse您可以访问StatusCode属性- https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.statuscode?view=netframework-4.7.2 #System_Net_HttpWebResponse_StatusCode ;

Just one thing to notice - you can't make HttpWebRequest NOT throw an exception when it recieves a status code that does not indicate success (all the more reason not to use this method). 只需注意一件事-当HttpWebRequest收到不表示成功的状态代码时,就不能使HttpWebRequest不引发异常(更重要的是不使用此方法)。 This means you have to be ready to catch the exception that will be thrown. 这意味着您必须准备好捕获将引发的异常。

So in the case of your example, it would be - 因此,以您的示例为例-

WebRequest SiteyeBaglantiTalebi06 = HttpWebRequest.Create("https://www.somesite.com/NotARealPath");
try
{
    WebResponse GelenCevap06 = SiteyeBaglantiTalebi06.GetResponse();
    // do things with the result
}
catch (WebException ex)
{
    using (WebResponse response = ex.Response)
    {
        HttpWebResponse asHttp = (HttpWebResponse)response;
        if (asHttp.StatusCode == System.Net.HttpStatusCode.NotFound)
        {
            // your 404 logic here
        }
        else 
        {
            // your "something went wrong but it's not a 404" logic 
        }
    }
}

As for making the code simpler - it's hard to understand exactly what you mean by that without understanding more about your program and what you're trying to do. 至于使代码更简单-在不了解程序和尝试执行的操作的情况下,很难准确理解您的意思。 In general, here are a few ideas - 通常,这里有一些想法-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM