web浏览器并没有得到DocumentCompleted事件 [英] WebBrowser doesn't get to DocumentCompleted event
问题描述
我在C#很新,我试图建立一个登录到网站,并返回它的源$ C $ C程序。问题是,我注册时,页面加载事件侦听器,但是当我调试它,它设置相同的事件后退出,实际上没有做什么,我希望它在网页加载之后做的。
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".
下面的源头 -
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\r\n");
Console.WriteLine("Done..");
Console.Read();
}
[STAThread]
static void Main(string[] args)
{
Program p = new Program();
p.Login();
}
}
}
从所有这些控制台输出,它只是变得到的与URL参数的浏览器实例,使用方法导航... 的
推荐答案
web浏览器要求你的程序泵消息循环。它不会以其他方式激发其事件。这是在一般用于使用单线程COM组件的任何程序的要求。或把它放在更容易理解的术语:你不能让一个程序是从控制台,并在同一时间起火事件,如DocumentCompleted忙于阅读。一个线程可以在时间只能做一件事。您可以通过自己与Application.Run写WinForms应用程序或启动一个泵消息循环()。有消息循环,一个线程的可以的做多一件事的时间。但是这并从你写的,现在有什么需要非常不同的code,你仍然无法使用到Console.ReadLine(),你会使用一个文本框来代替。
WebBrowser requires that your program pumps a message loop. It won't fire its events otherwise. This is in general a requirement for any program that uses a single-threaded COM component. Or to put it in more understandable terms: you can't get a program to be busy reading from the console and at the same time fire events like DocumentCompleted. A thread can do only one thing at time. You pump a message loop by writing a Winforms app or starting one yourself with Application.Run(). With a message loop, a thread can do more than one thing at a time. But that does require very different code from what you've written right now, you still can't use Console.ReadLine(), you'd use a TextBox instead.
您可以拯救你有什么在一个单独的线程运行浏览器,你会发现code你需要在的这个答案。
You can rescue what you have by running the browser in a separate thread, you'll find the code you need for that in this answer.
这篇关于web浏览器并没有得到DocumentCompleted事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!