0

So i have this code which i'm trying to find if Twitter / ? is in the source of the twitter.com/name ( to verify if the user exist) When the user exist, it works fine, when the user doesn't, it gives a 404 error and stop. is there anyway to ignore the 404 error and make it still look at the source code of that page? because if we go to ex: view-source:https://twitter.com/pogosode , it doesn't exist but we can still see the source..

foreach (Membre noms in p_nom)
        {
            string websiteName = "https://twitter.com/" + noms.NomMembre;
            string source = (new WebClient()).DownloadString(websiteName);
            if (source.Contains("<title>Twitter / ?</title>"))
                {
                p_disponible.Add(new MembreVerifier(noms.NomMembre));
                }
        }
leppie
  • 115,091
  • 17
  • 196
  • 297

1 Answers1

0

You can catch the WebException exception and read the response from the Response property.

Example:

foreach (Membre noms in p_nom)
{
    string source = "";
    try
    {           
        string websiteName = "https://twitter.com/" + noms.NomMembre;
        source = (new WebClient()).DownloadString(websiteName);     
    }
    catch (WebException ex)
    {
        using (var stream = ex.Response.GetResponseStream())
        {
            // Copy stream to buffer.
            var buffer = new byte[stream.Length];
            stream.Read(buffer, 0, (int)stream.Length);

            // Decode byte array to UTF-8 string.
            source = Encoding.UTF8.GetString(buffer);
        }
    }

    if (source.Contains("<title>Twitter / ?</title>"))
    {
         p_disponible.Add(new MembreVerifier(noms.NomMembre));
    }
}
Oskar Sjöberg
  • 2,728
  • 27
  • 31
  • Thanks! It works, the only thing is that i've made and else{*do something*} for when the source doesn't contain the twitter / ? but it doesn't do the things in the else – user2737579 Jan 02 '14 at 20:18
  • It is beacuse when an exception occurs (as it will when you get a 404) execution of the code will stop and the program jumps to the catch part. So you will have to modify your logic to account for that. – Oskar Sjöberg Jan 02 '14 at 20:21
  • Yea, i've added that : // TODO: Use content. if (content.Contains("Twitter / ?")) { //p_disponible.Add(new MembreVerifier(noms.NomMembre)); Console.WriteLine("{0} is available for twitter.", noms.NomMembre); } else { Console.WriteLine("Too bad! {0} isn't available for twitter.", noms.NomMembre); } into the catch and the else doesn't work :O – user2737579 Jan 02 '14 at 20:24
  • I've changed the code to better fit your purpose. – Oskar Sjöberg Jan 02 '14 at 20:32