没有表单的Web浏览器控件
问题描述:
我正在创建一个通用的反向链接检查器。它工作正常,除非主机试图阻止与noscript案件之类的东西刮擦。我的解决方案是在浏览器控件中加载页面,但我不能让dang的东西工作。它开始导航但似乎永远不会完成。
如果您觉得这样,请原谅我是不道德的。随意忽略我的问题。我发现它不是不道德的,因为除了检查页面是否有链接到我们自己的网站之外,我不会做任何其他事情。
Hi,
I am creating a generic "backlink" checker. It works fine unless the hosts try to prevent scraping with things like "noscript" cases. My solution is to load the page in a browser control, but I can't get the dang thing to work. It starts navigating but never seems to finish.
Forgive me if you feel this is unethical. Feel free to ignore my question. I don't find it unethical because I will not be doing anything else other than checking if the page has a link to our own sites.
引用:
seo和主机之间的军备竞赛一直持续
The arms race between seo and host rages ever onward
我尝试过:
What I have tried:
public string Window_Load(Uri url)
{
var e = new AutoResetEvent(false);
WebBrowser browser = new WebBrowser();
browser.Navigating += (sender, args) =>
{
// hits this
Console.WriteLine("navigating");
};
browser.Navigated += (sender, args) =>
{
// never hit
Console.WriteLine("navigated");
};
browser.DocumentCompleted += (sender, args) =>
{
// never hit
e.Set();
browser.Dispose();
};
browser.AllowNavigation = true;
browser.Navigate(url.ToString());
e.WaitOne();
return "";
}
答
对的调用e.WaitOne()
阻止当前线程,直到设置了AutoResetEvent
。WebBrowser
控件将引发其事件以响应在当前线程上处理的窗口消息。我相信你可以看到那里的小问题! :)
快速而肮脏的解决方案是使用Application.DoEvents
:
The call toe.WaitOne()
blocks the current thread until theAutoResetEvent
is set. TheWebBrowser
control will raise its events in response to window messages processed on the current thread. I'm sure you can see the slight problem there! :)
The quick-and-dirty solution would be to useApplication.DoEvents
:
browser.Navigate(url.ToString());
while (!e.WaitOne(0))
{
Application.DoEvents();
}