WebClient.DownloadString(url) when this url returns a 404 page, how can i skip this?

14,951

You will have to catch the Exception and test for a 404:

try
{
    string myString;
    using (WebClient wc = new WebClient())
        myString= wc.DownloadString("http://foo.com");

}
catch (WebException ex)
{
    if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
    {
        var resp = (HttpWebResponse)ex.Response;
        if (resp.StatusCode == HttpStatusCode.NotFound) // HTTP 404
        {
            //the page was not found, continue with next in the for loop
            continue;
        }
    }
    //throw any other exception - this should not occur
    throw;
}
Share:
14,951
Hasan Hüseyin Çakır
Author by

Hasan Hüseyin Çakır

Computer Engineer

Updated on June 28, 2022

Comments

  • Hasan Hüseyin Çakır
    Hasan Hüseyin Çakır almost 2 years

    I'm using WebClient.DownloadString(url) to download a web page, when a url a 404 web page it stops and doesn't work anymore. I want to skip these pages when I got this fault.

    if the url is 404 page, it doesn't start to download. so i can't parse the undownloaded data...