A lot of people are wondering if there is a easy way to interact with html forms within a C# application, the answer is yes
C# has a built-in class called WebBrowser which allows you to load and interact with webpages, the browser object is actually a internet explorer object and has about the same functionallity. The upside is that it doesn't include the IE gui, so you can make your own interface and have your own rules and restrictions.
Here I will show you a simple way of interacting with a web form, it pretty much explains itself and you can play around for yourself with the enourmous amounts of possibilities you have when using this class
The HTML form:
<html>
<head>
<title>My Web Form</title>
</head>
<body>
<form method="post" target="login.php" id="loginform">
<input type="hidden" name="event" value="login" />
<input type="text" name="username" id="username" /> Username
<input type="password" name="password" id="password" /> Password
<input type="submit" value="Login" />
</form>
</body>
</html>
This is what the form would look like in a browser window. Please note that I removed the submit button so you don't accidently click it.
Now its time to look at the C# code, as I said earlier it's pretty easy to understand but I'll throw in some comments as well
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'
}
}
This is what the webform would look like if you removed the call to the submit method
I will likely write another tutorial going deeper into the aspects of the WebBrowser object, hope you enjoyed it so far!