0

我创建了一个简单的工具,可以在网站中找到注册选项(200 个网站列表在数组列表中)。我使用的是 webbrowser,但它有缓存和 cookie 的问题,所以我切换到 webclient。当我放置断点和调试时它工作正常,但是当我正常运行它时,它还包括那些没有注册选项的网站。这是我的代码

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

定时器 1 代码

 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 代码:

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.");
        }
    }

请帮忙,如果有任何不挂gui的webclient替代品,请提出建议。泰。

4

1 回答 1

0

开始下载后立即处理 WebClient。

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

请帮忙,如果有任何不挂gui的webclient替代品

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

于 2013-08-23T17:42:10.980 回答