Search code examples
c#httpwebrequesthttp-status-code-404httpwebresponsesystem.net.httpwebrequest

How to Skip HttpWebResponse 404


Hi i have a program thats looking on some pages

for (int i=1; i < 1000; i++)
{
  string id = i.ToString();
  string url_source = get_url_source("http://mysite.com/"+id);
}

and a method

    public static string get_url_source(string url)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream());
        return sr.ReadToEnd();
    }

How should i change my method so the main program will just skip that url and continue to the next id when a 404-notfound occurs?

I changed my loop into this

try
            {
                string url_source = get_url_source(url);
            }
            catch(WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError)
                {
                    ConsoleBox.Text += i + ": " + WebExceptionStatus.ProtocolError.ToString() + "\r\n";
                    continue;
                }
                else
                {
                    throw;
                }
            }

Solution

  • Catch the error and check its status. If it is 404 - continue the loop, otherwise rethrow it.

    for (int i=1; i < 1000; i++)
    {
        string id = i.ToString();
        try
        {
             string url_source = get_url_source("http://mysite.com/"+id);
        }
        catch(WebException ex)
        {
            HttpWebResponse webResponse = (HttpWebResponse)ex.Response;          
            if (webResponse.StatusCode == HttpStatusCode.NotFound)
            {
                continue;
            }
            else
            {
                throw;
            }
        }
    
    }