I'm quite new at c# and am trying to build a program that logs into a website and returns it's source code. Problem is, I register an event listener for when the page is loaded, but when I debug it it quits after setting the same event, not actually doing what I want it to do after the page "loads".
Here's the source -
using System;
using System.Windows.Forms;
namespace WIN
{
class Program
{
string url = -snip-;
string username = -snip-;
string password = -snip-;
string task = -snip-;
string action = -snip-;
string timezone = -snip-;
private void Login()
{
Console.WriteLine("Started.");
Console.ReadLine();
Console.WriteLine("Declaring WebBrowser instance browser...");
WebBrowser browser = new WebBrowser();
Console.WriteLine("Done.");
Console.ReadLine();
Console.WriteLine("Registering an event for when the page finishes loading...");
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(pageLoaded);
Console.WriteLine("Done.");
Console.ReadLine();
Console.WriteLine("Using method Navigate of browser instance with url parameter...");
browser.Navigate(url);
Console.WriteLine("Done.");
Console.ReadLine();
}
private void pageLoaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Console.WriteLine("Declaring WebBrowser instance browser as sender...");
WebBrowser browser = sender as WebBrowser;
Console.WriteLine("Done.");
Console.ReadLine();
string response = browser.DocumentText;
Console.WriteLine("Searching for authenticity token...");
// looks in the page source to find the authenticity token.
// could also use regular expressions here.
int index = response.IndexOf("authenticity_token");
int startIndex = index + 41;
string authenticityToken = response.Substring(startIndex, 40);
Console.WriteLine("Found authenticity token.");
Console.WriteLine("Unregistering first event handler...");
// unregisters the first event handler
// adds a second event handler
browser.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(pageLoaded);
Console.WriteLine("Done.");
Console.WriteLine("Adding second event handler...");
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(pageLoaded2);
Console.WriteLine("Done.");
Console.Read();
Console.WriteLine("Formatting data to be posted to server...");
string postData = string.Format("_user={0}&_pass={1}&authenticity_token={2}&_task{3}&_action{4}&_timezone{5}", username, password, authenticityToken, task, action, timezone);
Console.WriteLine("Done.");
Console.Read();
Console.WriteLine("Declaring ASCIIEncoding instance enc...");
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
Console.WriteLine("Done.");
Console.Read();
// we are encoding the postData to a byte array
Console.WriteLine("Encoding postData to a byte array...");
browser.Navigate(url, "", enc.GetBytes(postData), "Content-Type: application/x-www-form-urlencoded
");
Console.WriteLine("Done..");
Console.Read();
}
[STAThread]
static void Main(string[] args)
{
Program p = new Program();
p.Login();
}
}
}
From all those console outputs, it only gets to Using method Navigate of browser instance with url parameter...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…