public class Form1
{
WebBrowser browser;
public Form1()
{
browser = new WebBrowser(); //Here we innitiate the browser object
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(OnBrowserLoad); //The event handler that will trigger when a webpage has fully loaded
browser.Navigate("mypage.htm", false); //here we navigate our browser to our html page, the false statement is to specify we don't want to open up a new window (we can do that after we have logged in, if we want to)
}
private void OnBrowserLoad(object sender, EventArgs e)
{
//Here we will simply fill out our form and submit it. You can also add more advanced algorithms that check that you got to the right page, and that you actually managed to login with the right credentials and so on. But that we will cover another time
browser.DocumentCompleted -= OnBrowserLoad; //We remove the eventhandler so that it won't trigger after login
var form = browser.Document.GetElementById("loginform"); //we need the form so we can submit it, though sites with more advanced login methods might require you to simulate a click on the submit button instead
browser.Document.GetElementById("username").SetAttribute("value", "MyUsername");
browser.Document.GetElementById("password").SetAttribute("value", "MyPassword");
//if we want to open a new window for the user when we login, we can add this
form.SetAttribute("target", "_blank");
//Finally we submit our form
form.InvokeMember("submit");
//If you wanted to invoke the submit method on a button, you just aquire the button by id or name and invoke the same member, 'submit'
}
}
{
WebBrowser browser;
public Form1()
{
browser = new WebBrowser(); //Here we innitiate the browser object
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(OnBrowserLoad); //The event handler that will trigger when a webpage has fully loaded
browser.Navigate("mypage.htm", false); //here we navigate our browser to our html page, the false statement is to specify we don't want to open up a new window (we can do that after we have logged in, if we want to)
}
private void OnBrowserLoad(object sender, EventArgs e)
{
//Here we will simply fill out our form and submit it. You can also add more advanced algorithms that check that you got to the right page, and that you actually managed to login with the right credentials and so on. But that we will cover another time
browser.DocumentCompleted -= OnBrowserLoad; //We remove the eventhandler so that it won't trigger after login
var form = browser.Document.GetElementById("loginform"); //we need the form so we can submit it, though sites with more advanced login methods might require you to simulate a click on the submit button instead
browser.Document.GetElementById("username").SetAttribute("value", "MyUsername");
browser.Document.GetElementById("password").SetAttribute("value", "MyPassword");
//if we want to open a new window for the user when we login, we can add this
form.SetAttribute("target", "_blank");
//Finally we submit our form
form.InvokeMember("submit");
//If you wanted to invoke the submit method on a button, you just aquire the button by id or name and invoke the same member, 'submit'
}
}