zoukankan      html  css  js  c++  java
  • asp.net使用WebBrowser采集加载完毕后的页面

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

    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()
        {
           try
            {
                var browser = new WebBrowser
                {
                    ScrollBarsEnabled = false,
                    ScriptErrorsSuppressed = true
                };
                browser.BringToFront(); 
                html = NavigateAndWaitForLoad(browser, new Uri(_url), 0);
            }
            catch (Exception ex)
            {}
        }
    
        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");

    写在最后: 

    asp.net使用多线程如果操作不好,容易造成iis崩溃

    asp.net中调用winform的WebBrowser,以上代码仅仅是能实现功能,至于能否满足高并发场景需求,需要实际测试下。

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

  • 相关阅读:
    如何重新加载 Spring Boot 上的更改,而无需重新启动服务器?
    FileUpload拦截器
    aspnet网页刷新
    查看SQL表的详细信息
    学习GDI+ (1)
    设计模式简单工厂模式
    对数据库表操作,统一的方法。
    随机产生300道四则运算
    略谈从计算机专业,再到软件构建的初识
    #在android studio中维护日程管理系统
  • 原文地址:https://www.cnblogs.com/shihao316558512/p/13087245.html
Copyright © 2011-2022 走看看