Library for working with web pages in C#
I'm in search for a library that will let me work with web pages using C# without having to display anything graph开发者_如何学运维ically. The library should handle web sites that use JavaScript / AJAX and it should return the correct HTML as if I were viewing the source from within Firefox/Chrome.
I've figured it out. It turns out I don't need a library at all and I can do it with the WebBrowser
control.
using System;
using System.Windows.Forms;
namespace WebBrowserDemo
{
class Program
{
public const string TestUrl = "http://www.w3schools.com/Ajax/tryit_view.asp?filename=tryajax_first";
[STAThread]
static void Main(string[] args)
{
WebBrowser wb = new WebBrowser();
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
wb.Navigate(TestUrl);
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey(true);
}
static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = (WebBrowser)sender;
HtmlElement document = wb.Document.GetElementsByTagName("html")[0];
HtmlElement button = wb.Document.GetElementsByTagName("button")[0];
Console.WriteLine(document.OuterHtml + "\n");
button.InvokeMember("Click");
Console.WriteLine(document.OuterHtml);
}
}
}
精彩评论