使用asp.net获取Web浏览器控件源

如何在页面后面的asp.net代码中使用WebBrowser()控件获取另一个Web站点的源代码。

到目前为止,我有这个,但没有任何选项导航或设置cookie或获取页面加载源? 你能帮忙吗?

 Imports System.Windows.Forms Partial Class _Default Inherits System.Web.UI.Page Dim a As WebBrowser = New WebBrowser() webBrowser1.Navigate("http://www.google.com") 'get the source End Class 

我得到了这个错误

ActiveX控件’8856f961-340a-11d0-a96b-00c04fd705a2’无法实例化,因为当前线程不在单线程单元中。

这将完美地运作

我们需要添加带有线程的webbrowser,否则我们将得到ActiveX控件’8856f961-340a-11d0-a96b-00c04fd705a2’无法实例化,因为当前线程不在单线程单元中。 错误

这是我们可以让webbrowser在asp.net网页中工作的方式

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Threading; using System.Windows.Forms; ///  /// Summary description for CustomBrowser ///  public class CustomBrowser { public CustomBrowser() { // // TODO: Add constructor logic here // } protected string _url; string html = ""; public string GetWebpage(string url) { _url = url; // WebBrowser is an ActiveX control that must be run in a // single-threaded apartment so create a thread to create the // control and generate the thumbnail Thread thread = new Thread(new ThreadStart(GetWebPageWorker)); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); string s = html; return s; } protected void GetWebPageWorker() { using (WebBrowser browser = new WebBrowser()) { // browser.ClientSize = new Size(_width, _height); browser.ScrollBarsEnabled = false; browser.ScriptErrorsSuppressed = true; browser.Navigate(_url); // Wait for control to load page while (browser.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents(); html = browser.DocumentText; } } } 

在网页中

 CustomBrowser browser = new CustomBrowser(); string s = browser.GetWebpage("http://localhost:8781/WebSite3/Default3.aspx"); Response.Write(s); 

你想要做的是在ASP.NET中略显不正统,但这里有一个例子可以帮助你:

http://www.codeproject.com/Articles/50544/Using-the-WebBrowser-Control-in-ASP-NET

当我在asp.net页面中使用webBrowser对象时,我发现了同样的错误。
但在我找到解决方案之后,

https://stackoverflow.com/a/1054408/900284

现在好了。