简体   繁体   English

c#asp.net使用Windows窗体WebBrowser

[英]c# asp.net use Windows Forms WebBrowser

I have a class of library code which uses a System.Windows.Forms.WebBrowser control to load a web page then extract information from the DOM tree, etc... I've found this to be the only reliable way to be able to parse information loaded onto a page with javascript, etc... 我有一类库代码,它使用System.Windows.Forms.WebBrowser控件加载网页,然后从DOM树中提取信息,等等。。。我发现这是唯一可靠的方法使用javascript等解析加载到页面上的信息...

This worked fine when testing with console app, but when I call the same method from an asp.net page (ASP.NET MVC Controller) it crashes with a "ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment."... i've tried adding [STAThread] to my method but that doesn't seem to help! 使用控制台应用程序进行测试时,此方法工作正常,但当我从asp.net页(ASP.NET MVC控制器)调用相同的方法时,它崩溃并显示为“ ActiveX控件'8856f961-340a-11d0-a96b-00c04fd705a2”,无法实例化因为当前线程不在单线程单元中。” ...我尝试将[STAThread]添加到我的方法中,但这似乎无济于事!

Any ideas? 有任何想法吗?

Thanks 谢谢

Update: 更新:

I need to allow a page to complete load and execute all its javascript, then search it for text that matches a template I have already stored... Don't think HtmlAgilityPack will work? 我需要让页面完成加载并执行所有的javascript,然后在页面中搜索与我已经存储的模板匹配的文本...不要认为HtmlAgilityPack可以工作吗? Does WatIn run the javascript? WatIn是否运行javascript?

Save yourself a lot of headache and use WatiN instead of a WebBrowser control. 省去很多麻烦,并使用WatiN代替WebBrowser控件。

See this question: Best method for Website Automation? 看到以下问题: 网站自动化的最佳方法?

I believe it is impossible to get IIS to run in single thread apartment model (STA). 我相信让IIS在单线程单元模型(STA)中运行是不可能的。 Instead of using the WebBrowser control, you could take a look at the HTML Agility Pack . 除了使用WebBrowser控件外,您还可以查看HTML Agility Pack It will let your read in a web page and poke around the DOM. 它可以让您阅读网页并在DOM中四处浏览。 The WebBrowser control uses lots of computer resources that you won't have to worry about it make the switch. WebBrowser控件使用大量的计算机资源,您不必担心会进行切换。

Everything in the System.Windows is intended for desktop, not web stuff. System.Windows均适用于桌面,而不适用于Web。 I agree with Jake Pearson that you should use something else. 我同意杰克·皮尔森(Jake Pearson)的观点,您应该使用其他内容。 But if you want you can try forcing the page into STA mode using this directive: 但是,如果您愿意,可以尝试使用以下伪指令将页面强制进入STA模式:

<%@Page AspCompat="true"%>

http://msdn.microsoft.com/en-us/library/zwk9h2kb.aspx http://msdn.microsoft.com/en-us/library/zwk9h2kb.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM