|
贴上代码
public class WebBrowserCrawler
{
private WebBrowser _webbrowder;
private string _result { get; set; }
private string _path { get; set; }
public string GetReult(string url)
{
_path = url;
var mThread = new Thread(FatchDataToResult);
mThread.SetApartmentState(ApartmentState.STA);
mThread.Start();
mThread.Join();
return _result;
}
private void FatchDataToResult()
{
_webbrowder = new WebBrowser();
_webbrowder.DocumentCompleted += _webbrowder_DocumentCompleted;
_webbrowder.Url = new Uri(_path);
while (_webbrowder.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
_webbrowder.Dispose();
}
void _webbrowder_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument doc = (sender as WebBrowser).Document;
HtmlElement uinList = doc.GetElementById("list_uin");
_result = uinList.OuterHtml;
}
}
调用
WebBrowserCrawler wc = new WebBrowserCrawler();
string str = wc.GetReult(url);
在VS里面测试运行正常,挂到IIS6.0上面就出问题。网页一直加载无反应。
错误信息:
Application: w3wp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Runtime.InteropServices.COMException
Stack:
at System.Windows.Forms.WebBrowser.PerformNavigate2(System.Object ByRef, System.Object ByRef, System.Object ByRef, System.Object ByRef, System.Object ByRef)
at System.Windows.Forms.WebBrowser.set_Url(System.Uri)
at msjy.game.WebBrowserCrawler.FatchDataToResult()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.runTryCode(System.Object)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
|
|