码迷,mamicode.com
首页 > 编程语言 > 详细

asp.net使用WebBrowser采集加载完毕后的页面(线程安全)

时间:2020-06-10 18:48:04      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:tostring   wait   hat   desc   err   start   enabled   errors   over   

工具类代码:(代码可以自己整理下,这里重点在实现方式)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Windows.Forms;

/// <summary>
/// Summary description for CustomBrowser
/// </summary>
public class CustomBrowser
{
    public CustomBrowser()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    protected string _url;
    string html = "";
    public string GetWebpage(string url)
    {
        _url = url;
        // WebBrowser is an ActiveX control that must be run in a
        // single-threaded apartment so create a thread to create the
        // control and generate the thumbnail
        Thread thread = new Thread(new ThreadStart(GetWebPageWorker));
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        thread.Join();
        string s = html;
        return s;
    }
    protected void GetWebPageWorker()
    {
        var browser = new WebBrowser
        {
            ScrollBarsEnabled = false,
            ScriptErrorsSuppressed = true
        };
        browser.BringToFront(); 
        html = NavigateAndWaitForLoad(browser, new Uri(_url), 0);
    }

    private string NavigateAndWaitForLoad(WebBrowser browser, Uri uri, int waitTime)
    {
        const int sleepTimeMiliseconds = 5000;

        browser.Navigate(uri);
        var count = 0;

        while (browser.ReadyState != WebBrowserReadyState.Complete)
        {
            Thread.Sleep(sleepTimeMiliseconds);
            Application.DoEvents();
            count++;

            if (count > waitTime / sleepTimeMiliseconds)
            {
                break;
            }
        }

        while (browser.Document.Body == null)
        {
            Application.DoEvents();
        }

        return browser.Document.Body.OuterHtml.ToString();
    }
}

 

调用方法:

new CustomBrowser().GetWebpage("http://www.baidu.com");

 

参考链接:https://stackoverflow.com/questions/10313369/taking-screenshot-of-an-iframe-on-button-click/10315397#10315397

 

asp.net使用WebBrowser采集加载完毕后的页面(线程安全)

标签:tostring   wait   hat   desc   err   start   enabled   errors   over   

原文地址:https://www.cnblogs.com/shihao316558512/p/13087245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!