将webbrowser用于多个页面

时间:2015-08-03 13:35:10

标签: c# sql-server

我正在开发一个项目。在我的mssql服务器上,我有很多网址,我需要检查一下是否可以登录。到目前为止,这是我的代码:

        public List<WebBrowser> myBrowsers = new List<WebBrowser>();

    private void button3_Click(object sender, EventArgs e)
    {
        SqlConnection connection = new SqlConnection(connectionString);
        connection.Open();
        SiteeeID = Convert.ToInt32(listBox1.SelectedValue);
        string url = "select http,username,pass,userID,passID,butonID from AyrintiSite where username!=''";
        DataTable dt = new DataTable();
        using (SqlConnection cn = new SqlConnection(connectionString))
        {
            using (SqlCommand cm = new SqlCommand(url, cn))
            {
                cn.Open();

                dt.Load(cm.ExecuteReader());

            }
        }

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            WebBrowser browser = new WebBrowser();
            browser.DocumentCompleted += webBrowserDocumentCompleted;
            browser.Navigate(dt.Rows[i][0].ToString());
            myBrowsers.Add(browser);
        }
    }
    private void webBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

        if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
            return;

        WebBrowser browser = sender as WebBrowser;

        browser.Document.GetElementById("KullaniciAdiTextBox").SetAttribute("value", "username");
        browser.Document.GetElementById("SifreTextBox").SetAttribute("value", "pass");
        browser.Document.GetElementById("SistemeGirImageButton").InvokeMember("click");
        browser.DocumentCompleted -= webBrowserDocumentCompleted;
        browser.DocumentCompleted += webBrowserDestroyOnCompletion;
    }
    private void webBrowserDestroyOnCompletion(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
            return;

        if (e.Url.AbsolutePath.Contains("Login"))
        {
            //send e-mail
        }
        WebBrowser browser = sender as WebBrowser;
        browser.Dispose();
        myBrowsers.Remove(browser);

    }

现在我的数据表中有17页。其中10-12个可以正确登录,但其他人不能和我的程序发送电子邮件。我检查了数千次,这是我数据库中的全部,所有人都应该登录。我的代码有错误吗?

1 个答案:

答案 0 :(得分:0)

导航到DocumentCompleted事件中的下一个网址

声明一个pageIndex变量以使索引保持在dt.rows:

 int pageIndex=0 ; 

用以下内容替换dt.rows上的循环:

        WebBrowser browser = new WebBrowser();
        browser.DocumentCompleted += webBrowserDocumentCompleted;
        browser.Navigate(dt.Rows[pageIndex][0].ToString());

&#34;环路&#34;在DocumentCompleted事件中的dt.rows:

private void webBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath) return;
    WebBrowser browser = sender as WebBrowser;
    browser.Document.GetElementById( "KullaniciAdiTextBox").SetAttribute("value", "username");
    browser.Document.GetElementById( "SifreTextBox").SetAttribute("value", "pass");
    browser.Document.GetElementById( "SistemeGirImageButton").InvokeMember("click");
    pageIndex++ ;
    If (pageIndex<dt.Rows.Count) browser.Navigate(dt.Rows[pageIndex][0].ToString());
  }