一個瀏覽器迴圈重新整理網頁的例子

來源:互聯網
上載者:User

標籤:style   blog   class   code   java   tar   

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)

    

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.