Over the period of time C# really became a mature programming language. Compared to other standard programming language it's new but it has already offered lots to the programmer and many more new features are being added in Framework 4.0.
I was exploring today, how I can work with websites like "Gmail" through my program. This is not a hack but a standard way of logging into the website, to do our work and logout. In this section, I tried to show, using the C#.net 'web browser' class, how anyone can log in to the site by providing a username/password and logging out. In this section I've not shown any other operation, I'll try to cover that in the future.
Reference: Website Login
- Add the WebBrowser control on a WinForm
- Add three Buttons, one for UserName/PWD, the second button for login, and the third button for logout from Gmail.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WebBrowser { public partial class Form1 : Form { public Form1() { InitializeComponent(); webBrowser1.Url = new Uri("http://www.gmail.com"); } private void usernamepwd_Click(object sender, EventArgs e) { HtmlElementCollection theElementCollection; theElementCollection = webBrowser1.Document.GetElementsByTagName("input"); foreach (HtmlElement curElement in theElementCollection) { String str = (curElement.GetAttribute("id")).ToString(); if(str == "Email") { curElement.SetAttribute("value", "username"); } if (str == "Passwd") { curElement.SetAttribute("value", "password"); } } } private void login_Click(object sender, EventArgs e) { HtmlElementCollection theElementCollection; theElementCollection = webBrowser1.Document.GetElementsByTagName("input"); foreach (HtmlElement curElement in theElementCollection) { String str = (curElement.GetAttribute("id")).ToString(); if (str == "signIn") { curElement.InvokeMember("click"); } } } private void logout_Click(object sender, EventArgs e) { HtmlElementCollection theElementCollection; // Sign out is a link, so tag name search by 'a'. theElementCollection = webBrowser1.Document.GetElementsByTagName("a"); foreach (HtmlElement curElement in theElementCollection) { String str = curElement.InnerText; if (str == "Sign out") { curElement.InvokeMember("click"); } } } } }
Reference: Website Login
Comments