C. NET Method for Automatic Login of Web Pages
- 2021-08-12 03:22:30
- OfStack
In this paper, an example is given to describe the method of C #. NET to realize automatic login of web pages. Share it for your reference. The details are as follows:
Using C # language to write an Windows Form application program to automatically log in to a specific page.
Take automatic login: http://localhost/Web/Login.aspx as an example to explain how to simulate manual input of user name and password and click Login to realize automatic login.
Create a new C # application, give the application a name, such as AutoLogin, add an TextBox, Button, and WebBrowser control to the form, and add an webBrowser1_DocumentCompleted event to the WebBrowser control.
The button click event and the code for webBrowser1_DocumentCompleted are as follows:
private void btn_Add_Click(object sender, EventArgs e)
{
string sUrl = txb_Url.Text.Trim();
if (sUrl.Length > 0)
{
webBrowser1.Navigate(sUrl);
}
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement ClickBtn = null;
if (e.Url.ToString().ToLower().IndexOf("login.aspx") > 0)
{
HtmlDocument doc = webBrowser1.Document;
for (int i = 0; i < doc.All.Count; i++)
{
if(doc.All[i].TagName.ToUpper().Equals("INPUT"))
{
switch(doc.All[i].Name)
{
case "txtUserName":
doc.All[i].InnerText = "xxxx@yy.com"; // User name
break;
case "txtPassword":
doc.All[i].InnerText = "zzzzzz"; // Password
break;
case "btnSubmit":
ClickBtn = doc.All[i];
break;
}
}
}
ClickBtn.InvokeMember("Click"); // Click the "Login" button
}
}
When you enter http://localhost/Web/Login. aspx in TextBox and click the button, you can realize the automatic login of the page.
I hope this article is helpful to everyone's C # programming.