简体   繁体   中英

C# Webclient not working properly

I've created a simple tool that can find Sign up option in websites (200 website list is in arraylist). I was using webbrowser but it has a problem of cache and cookie so i switched to webclient. It works fine when i put breakpoints and debug but when i run it normally, it also include those websites which doesn't have signup option. Here is my code

private void btnSearch_Click(object sender, EventArgs e)
    {           
            timer1.Enabled = true;
            timer1.Start();
    }

Timer1 code

 string st;
        private void timer1_Tick(object sender, EventArgs e)
        {
                st = "";
Application.DoEvents();                
                        try
                        {
                            st = lst[dataindex2].ToString();       
                            using (WebClient asyncWebRequest = new WebClient())
                            {
                                asyncWebRequest.DownloadDataCompleted += asyncWebRequest_DownloadDataCompleted;
                                Uri urlToRequest = new Uri(st);
                                asyncWebRequest.DownloadDataAsync(urlToRequest);
                                asyncWebRequest.Dispose();
                            }

                            dataindex2++;
                            if (dataindex2 == lst.Count)
                            {
                                timer1.Stop();                                
                                lblStatus.Text = "Stopped";
                                lblStatus.ForeColor = Color.DarkRed;
                                MessageBox.Show("Search Completed");                                
                            }
                        }
                        catch (Exception ex)
                        {
                            timer1.Stop();                            
                            lblStatus.Text = "Stopped";
                            lblStatus.ForeColor = Color.DarkRed;
                            timer1.Dispose();
                            MessageBox.Show(ex.Message);
                            return;
                        }

asyncWebRequest_DownloadDataCompleted code:

private void asyncWebRequest_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            timer1.Stop();
            ena();
            lblStatus.Text = "Stopped";
            lblStatus.ForeColor = Color.DarkRed;
            timer1.Dispose();                
            MessageBox.Show(e.Error.Message);                
        }

        if (e.Result != null && e.Result.Length > 0)
        {
            string browsetext = "";
            int = iSuccess = 0;
            browsetext = Encoding.Default.GetString(e.Result);

                    iSuccess = browsetext.IndexOf("Sign up") + 1;
                    if (iSuccess == 0)
                    {

                    }
                    else
                    {

                        listBox1.Items.Add(st);
                        domaincount++;                            
                        lblDomainCount.ForeColor = Color.DarkGreen;
                        lblDomainCount.Text = domaincount.ToString();
                    }
                }
                else
                {
                }
            }
        }
        else
        {
            MessageBox.Show("No data found.");
        }
    }

Please help and if there is any alternate of webclient that doesn't hang gui then please suggest. ty.

You dispose WebClient as soon as you start the download.

asyncWebRequest.DownloadDataAsync(urlToRequest);
asyncWebRequest.Dispose();

Please help and if there is any alternate of webclient that doesn't hang gui

see my other answer which creates a wrapper for WebClient to be able to use async/await. HttpClient can be an alternative too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM