zoukankan      html  css  js  c++  java
  • Webbrowser 在web项目中的使用

     1  
     2         string htmlstr = string.Empty;
     3 [STAThread]
     4         public string GetHtmlByWeb(string url)
     5         {
     6             try
     7             {
     8 
     9                 RunWithSingleThread(url);
    10                 DateTime dtime = DateTime.Now;
    11                 double timespan = 0;
    12 //等待 页面加载完毕 并获取到参数
    13                 while (string.IsNullOrWhiteSpace(htmlstr) && timespan < 10)
    14                 {
    15                     DateTime time2 = DateTime.Now;
    16                     timespan = (time2 - dtime).TotalSeconds;
    17                 }
    18                 // double lo = DateTime.Now.Subtract(dtime).TotalSeconds;
    19             }
    20             catch (Exception)
    21             {
    22               
    23             }
    24             finally
    25             {
    26                 if (t != null && t.ThreadState == ThreadState.Running)
    27                 {
    28                     t.Abort();
    29                 }
    30             }
    31            
    32             return htmlstr;
    33         }
    34 
    35 
    36 //线程
    37  private Thread t;
    38 //异步执行WebBrowser
    39         public void RunWithSingleThread(object url)
    40         {
    41             ParameterizedThreadStart ps = new ParameterizedThreadStart(GetHtmlWithBrowser);
    42             t = new Thread(ps);
    43             t.IsBackground = true;
    44             t.ApartmentState = ApartmentState.STA;
    45             t.Start(url);
    46         }  
    47 
    48 private WebBrowser wb;
    49         private void GetHtmlWithBrowser(object url)
    50         {
    51             htmlstr = string.Empty;
    52 
    53             wb = new WebBrowser();
    54             wb.ScriptErrorsSuppressed = true;  //防止脚本异常跳出弹窗
    55             wb.ScrollBarsEnabled = true;
    56             wb.Navigate(url.ToString());
    57             
    58             wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
    59             while (wb.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
    60             {
    61                 System.Windows.Forms.Application.DoEvents(); //避免假死,若去掉则可能无法触发 DocumentCompleted 事件。
    62             }
    63 
    64         }
    65 //获取内容
    66  public void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    67         {
    68             htmlstr = wb.Document.Body.InnerHtml;
    69 
    70         }
  • 相关阅读:
    Android特效专辑(一)——水波纹过渡特效(首页)
    简单整数算术运算
    Python 获取新浪微博的热门话题 (API)
    比真机还快的Android模拟器——Genymotion
    兔子--CheckBox与Radiobutton的差别
    hdu oj 3127 WHUgirls(2009 Asia Wuhan Regional Contest Online)
    python中列表,元组,字符串如何互相转换
    典型LoadRunner脚本
    LoadRunner显示中文乱码的问题
    如何解决paramiko执行与否的问题
  • 原文地址:https://www.cnblogs.com/dxqNet/p/8514520.html
Copyright © 2011-2022 走看看