zoukankan      html  css  js  c++  java
  • 一个浏览器循环刷新网页的例子

    class Program
        {
            public static void Main(string[] args)
            {
                int i=0;
                String[] urlarray = new String[] { "http://www.baidu.com/", "http://msdn.microsoft.com/", "http://www.qidian.com/Default.aspx" };
                RefreshPage repa = new RefreshPage(0, 1, urlarray, false);
                while (i < 7)
                {
                    repa.Next();
                    repa.FindCurrent = "No";
                    i++;
                }
            }      
        }
    View Code
     class RefreshPage
        {
            int CurrentPage; 
            int NextPage;
            String[] UrlArray;
            public String FindCurrent="No";
            bool Stop;
            public RefreshPage(int CurrentPage, int NextPage, String[] UrlArray, bool Stop)
            {
                this.CurrentPage = CurrentPage;
                this.NextPage = NextPage;
                this.UrlArray = UrlArray;
                this.Stop = Stop;
            }
            public void Next()
            {
                SHDocVw.ShellWindows sw = new SHDocVw.ShellWindows();
                while (FindCurrent == "No")
                {
                    foreach (SHDocVw.InternetExplorer Iweb in sw)
                    {
    
                        if (Iweb.LocationURL.StartsWith(UrlArray[CurrentPage]))
                        {
                            HTMLDocument doc2 = Iweb.Document as HTMLDocument;
                            HTMLScriptElement script = (HTMLScriptElement)doc2.createElement("script");
                            script.text = "function newDoc(url){window.location.assign(url)} t=setTimeout("newDoc(" + "'" + UrlArray[NextPage] + "'" + ")", 5000)";
                            HTMLBody body = doc2.body as HTMLBody;
                            if (body != null)
                            {
                                body.appendChild((IHTMLDOMNode)script);
                                FindCurrent = "Yes";
                            }
                            else
                                continue;
                        }
                    }
                }
                CurrentPage+= 1;
                NextPage += 1;
                if (NextPage >= UrlArray.Length)
                    NextPage = 0;
                if (CurrentPage >= UrlArray.Length)
                    CurrentPage = 0;
                System.Console.WriteLine("current");
            }
        }
    View Code

    1. 首先需要引用两个dll

    SHDocVw.dll

    mshtml.dll

    2. 本例子是按照所给出的数组,数组中存储的是将要循环访问的url,程序访问打开的网页寻找与数组的第一个url匹配的值,并且给他添加脚本,使该网页跳转到另一个url,即

    数组的下一个值,使数组中所有url循环访问到。


    3. 变量FindCurrent非常重要,由于浏览器打开一个网页需要时间,而程序读取确是,很快的,如果没有这个变量的判断,则代码会一直读不到当前要寻找的url。

    while (FindCurrent == "No")

    同理读取到当前url,但是html内容还没有load完,也无法读到html 的body,会导致一直读不到html body,下面这个判断也非常重要

    if (body != null)

        

  • 相关阅读:
    matlab程序性能优化与混合编程技术介绍
    最大熵原理/最大熵原则/最大熵模型(the maximum entropy principle,MEP)
    马氏距离 Mahalanobis Distance
    时间序列分析
    Windows XP + Apache 2.2.4 + PHP 5.2.0 + MySQL 5.0.27 + Zend Optimizer 3.2.0环境配置方法
    栈应用——表达式求值
    Android实现模拟时钟(简单+漂亮)时针、分针、秒针
    基于循环链表的约瑟夫问题
    assert()详解
    Hadoop HPROF 的使用
  • 原文地址:https://www.cnblogs.com/bianren/p/3718236.html
Copyright © 2011-2022 走看看