简体   繁体   中英

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.

  • It will get product prices.
  • But when product stock is 0. They are closing the page.

My program adding prices to listbox. When page giving 404 It should add empty listbox item.

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).

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 ;

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). 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 -

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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