C# : “How to use WebBrowser and print the html to the console. ”

醉酒当歌 提交于 2020-01-17 06:47:20

问题


I have for plus 10 hours now tried to get a hole through to the WebBrowser property.

I am simply trying to navigate to google.com and print the html code to the console. This constantly gives me a null reference.

There is alot of questions related to this and they all say that a DocumentCompleted Eventhandler is needed or not setup properly.

So i tried this to the best of my ability though still no luck. I then went so far as to copy paste the official example from Microsoft I am still getting the null reference!

    using System;
    using System.Windows.Forms;


    namespace Aamanss
    {
        class MainClass
        {
            public static void PrintHelpPage()
            {
                // Create a WebBrowser instance. 
                WebBrowser webBrowserForPrinting = new WebBrowser();

                // Add an event handler that prints the document after it loads.
                webBrowserForPrinting.DocumentCompleted +=
                    new WebBrowserDocumentCompletedEventHandler(PrintDocument);

                // Set the Url property to load the document.
                webBrowserForPrinting.Url = new Uri("https://www.google.com");
            }

            public static void PrintDocument(object sender,
                WebBrowserDocumentCompletedEventArgs e)
            {
                // Print the document now that it is fully loaded.
                ((WebBrowser)sender).Print();

                // Dispose the WebBrowser now that the task is complete. 
                ((WebBrowser)sender).Dispose();
            }

            public static void Main(string[] args)
            {
                PrintHelpPage();
            }
        }

}

As you can see the majority of the above code is copy pasted from Microsoft. And this errors:

Gtk-Message: Failed to load module "atk-bridge"
libgluezilla not found. To have webbrowser support, you need libgluezilla installed        
System.NullReferenceException: Object reference not set to an instance of an object
      at System.Windows.Forms.WebBrowser.Navigate (System.Uri url) [0x0000e] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:304 
      at System.Windows.Forms.WebBrowser.set_Url (System.Uri value) [0x00007] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:231 
      at (wrapper remoting-invoke-with-check) System.Windows.Forms.WebBrowser:set_Url (System.Uri)
      at Aamanss.MainClass.PrintHelpPage () [0x00024] in /root/Projects/Aamanss/Aamanss/Program.cs:19 
      at Aamanss.MainClass.Main (System.String[] args) [0x00001] in /root/Projects/Aamanss/Aamanss/Program.cs:34 
    [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
      at System.Windows.Forms.WebBrowser.Navigate (System.Uri url) [0x0000e] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:304 
      at System.Windows.Forms.WebBrowser.set_Url (System.Uri value) [0x00007] in /run/build/mono/mcs/class/System.Windows.Forms/System.Windows.Forms/WebBrowser.cs:231 
      at (wrapper remoting-invoke-with-check) System.Windows.Forms.WebBrowser:set_Url (System.Uri)
      at Aamanss.MainClass.PrintHelpPage () [0x00024] in /root/Projects/Aamanss/Aamanss/Program.cs:19 
      at Aamanss.MainClass.Main (System.String[] args) [0x00001] in /root/Projects/Aamanss/Aamanss/Program.cs:34

I am really desperate to figure out how you make a WebBrowser Navigation work in general and therefore i sincerely hope one of you can illustrate this for dummies.


回答1:


OK,

I misunderstood the problem that its a windows application (Winforms)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintHelpPage();
            Console.ReadKey();
        }

        public static void PrintHelpPage()
        {
            var th = new Thread(() => {
                var br = new WebBrowser();
                br.DocumentCompleted += PrintDocument;
                br.Navigate("http://google.com");
                Application.Run();
            });
            th.SetApartmentState(ApartmentState.STA);
            th.Start();
        }

        public static void PrintDocument(object sender,
            WebBrowserDocumentCompletedEventArgs e)
        {
            var browser = sender as WebBrowser;
            // Print the document now that it is fully loaded.
            browser.Print();

            // Dispose the WebBrowser now that the task is complete. 
            browser.Dispose();
        }
    }
}

the issue is that a console application would not fire the DocumentCompletedEvent unless you explicitly mark it STAThread as i did in the thread.



来源:https://stackoverflow.com/questions/43517749/c-sharp-how-to-use-webbrowser-and-print-the-html-to-the-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!