Using a web browser for multiple pages

I'm working on the project. I have so many urls on my mssql server and I need to check if I can login or not. Here is my code:

        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);

    }

      

now i have 17 pages in my datatable. 10-12 of them can log in correctly, but others cannot, and my program sends me an email. I checked a thousand times everything that was in my database, they should all be logged in. Are there any mistakes in my code?

+3


source to share


1 answer


Go to the next URL in the DocumentCompleted event

Declare a variable pageIndex to store the index in dt.rows:

 int pageIndex=0 ; 

      

Replace the loop with dt.rows like this:



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

      

"Loop" to dt.rows in the DocumentCompleted event:

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());
  }

      

0


source







All Articles