提问人:Nulle 提问时间:4/20/2017 最后编辑:Nulle 更新时间:4/20/2017 访问量:5176
C#:“如何使用WebBrowser并将html打印到控制台。
C# : "How to use WebBrowser and print the html to the console. "
问:
我已经花了10多个小时试图把一个洞通给酒店。WebBrowser
我只是尝试导航到 google.com 并将 html 代码打印到控制台。这不断给我一个空引用。
有很多与此相关的问题,他们都说需要 DocumentCompleted 事件处理程序或未正确设置。
所以我尽我所能尝试了这个,尽管仍然没有运气。然后我甚至复制粘贴了 Microsoft 的官方示例,我仍然得到空引用!
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();
}
}
}
如您所见,上述大部分代码都是从 Microsoft 复制粘贴的。而这个错误:
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
我真的很想弄清楚你是如何使WebBrowser导航工作的,因此我真诚地希望你们中的一个人可以为傻瓜说明这一点。
答:
3赞
Digvijay
4/20/2017
#1
还行
我误解了它是 Windows 应用程序 (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();
}
}
}
问题是控制台应用程序不会触发 DocumentCompletedEvent,除非您像我在线程中那样显式将其标记为 STAThread。
评论
0赞
Nulle
4/20/2017
我仍然得到一个空引用。
0赞
Nulle
4/20/2017
很抱歉,我仍然得到一个空引用。你自己测试过吗?
0赞
Digvijay
4/20/2017
是的,你有没有检查过行 // 设置 Url 属性以加载文档。webBrowserForPrinting.Navigate(“google.com”);
0赞
Nulle
4/20/2017
这是我的代码现在的样子。仍然得到一个空引用。pastebin.com/jEAnh04Y
1赞
Digvijay
4/20/2017
我正在 visual studio 而不是 monoDevelop 中尝试这个,但我相信原理是一样的!我不会发表更多评论,否则它看起来像一个聊天:)。我希望我能帮上忙!
评论
curl www.google.com
HttpWebRequest