samedi 21 janvier 2017

Why am i getting web timeout?

The code downloads URL-text content (stackoverflow post) in which a string is being looked for and once found, I get notified with 'console.writeline'

The purpose of this console app is to get a notification when a stackoverflow's post gets an answer, however, after this code runs, it then gives me the 'timeout' error, how do I fix this?

public static int count { get; set; }

    public static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        //DOWNLOAD TEXT FROM SO.COM
        System.Net.WebClient wc = new System.Net.WebClient();
        byte[] raw = wc.DownloadData("http://ift.tt/1WZE6u6");

        string webData = System.Text.Encoding.UTF8.GetString(raw);



        //FIND ALL OCCURENCES OF '<tc class="answercell">'
        string pattern = "<td class=\"answercell\">";
        Regex rx = new Regex(pattern, RegexOptions.None);
        MatchCollection mc = rx.Matches(webData); //4


        //NOTIFY WHEN AN ANSWER FOR THAT POST'S QUESTION IS FOUND
        List<string> LS1 = new List<string>();

        foreach (Match m in mc)
        {
            LS1.Add(m.Value);
            Console.WriteLine(m.Value);
        }

        if (LS1.Count >= count)
        {
            Console.WriteLine("count: " + count);
            Console.WriteLine("LS1 count: " + LS1.Count);

            count = count + 1;
        }
    }


    static void Main(string[] args)
    {

        //CHECK EVERY .5 SECONDS
        System.Timers.Timer aTimer = new System.Timers.Timer();
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        aTimer.Interval = 500;
        aTimer.Enabled = true;

        Console.ReadKey();


    }




Aucun commentaire:

Enregistrer un commentaire