[英]On change event on WebBrowser in c#
WebBrowser是否有任何“重新加载”事件?
我的意思是某个事件,它将在页面本身重新加载时将一些调试消息打印到控制台。
您可以收听DocumentCompleted事件,并打印到控制台。 每次完成文档下载后都会触发此事件,因此对于1个URL可能会触发多次。 只需确保您检查DocumentCompleted的Url属性并将其与您的URL进行比较即可。
要打印调试消息:
System.Diagnostics.Debug.WriteLine("Send to debug output.");
编辑:更多信息: https : //msdn.microsoft.com/zh-cn/library/system.windows.forms.webbrowser.documentcompleted(v=vs.110).aspx
protected void Page_Load(object sender, EventArgs e)
{
if (Session["NextLoad"] == true)
{
//save to log
}else
{
Session["NextLoad"] = true;
}
}
您也可以基于回发。
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
//save to log
}
}
或者,如果您需要保持会话状态,并且不想使用cookie,则将用户的ip粘贴到数据库中,并在下次访问时检查它是否存在。 如果是这样,请写入日志。
protected void Page_Load(object sender, EventArgs e)
{
if(!does_User_IP_Exist_In_DB(user_IP)){
add_User_IP(user_IP);
}else{
write_To_Log();
}
}
使用FRAMES会改变一切。 您将要使用:
// event handler for when a document (or frame) has completed its download
Timer m_pageHasntChangedTimer = null;
private void webBrowser_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e ) {
// dynamic pages will often be loaded in parts e.g. multiple frames
// need to check the page has remained static for a while before safely saying it is 'loaded'
// use a timer to do this
// destroy the old timer if it exists
if ( m_pageHasntChangedTimer != null ) {
m_pageHasntChangedTimer.Dispose();
}
// create a new timer which calls the 'OnWebpageReallyLoaded' method after 200ms
// if additional frame or content is downloads in the meantime, this timer will be destroyed
// and the process repeated
m_pageHasntChangedTimer = new Timer();
EventHandler checker = delegate( object o1, EventArgs e1 ) {
// only if the page has been stable for 200ms already
// check the official browser state flag, (euphemistically called) 'Ready'
// and call our 'OnWebpageReallyLoaded' method
if ( WebBrowserReadyState.Complete == webBrowser.ReadyState ) {
m_pageHasntChangedTimer.Dispose();
OnWebpageReallyLoaded();
}
};
m_pageHasntChangedTimer.Tick += checker;
m_pageHasntChangedTimer.Interval = 200;
m_pageHasntChangedTimer.Start();
}
OnWebpageReallyLoaded() {
/* place your harvester code here */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.