Pergunta

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;
                }
            }
Foi útil?

Solução

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

}

Outras dicas

You need to catch and the WebException that is thrown when you do GetResponseStream

try
{
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream());
    return sr.ReadToEnd();
}
catch (WebException webEx)
{
    //If you want only 404, some logic here
    //if (((HttpWebResponse)webEx.Response).StatusCode == HttpStatusCode.NotFound)
    //{
    //}
    //else
    //{
    //      throw;
    //}
    //webEx.Status;
}

try: if (response.StatusCode == HttpStatusCode.OK)

Use a try/catch, with WebException as exception type. you can test the status :

 ex.Response.StatusCode==HttpStatusCode.NotFound
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top