tags:

views:

64

answers:

1

I'm in search for a library that will let me work with web pages using C# without having to display anything graphically. 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.

A: 

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);           
        }
    }
}
kitchen
You may want to de-reference your event handler when you clean up the form, in case your 'app' gets bigger.
George Stocker
@kitchen: I'm pretty sure DocumentCompleted will be called once the initial load is completed, not once the subsequent AJAX call has completed. Have you had success with this?
Michael Shimmins
@Michael: Yes, it works when the AJAX call completes. @George: This is just a demo, the actual app is cleaner/more proper :)
kitchen