string htmlstr = string.Empty;
[STAThread]
public string GetHtmlByWeb(string url)
{
try
{ RunWithSingleThread(url);
DateTime dtime = DateTime.Now;
double timespan = ;
//等待 页面加载完毕 并获取到参数
while (string.IsNullOrWhiteSpace(htmlstr) && timespan < )
{
DateTime time2 = DateTime.Now;
timespan = (time2 - dtime).TotalSeconds;
}
// double lo = DateTime.Now.Subtract(dtime).TotalSeconds;
}
catch (Exception)
{ }
finally
{
if (t != null && t.ThreadState == ThreadState.Running)
{
t.Abort();
}
} return htmlstr;
} //线程
private Thread t;
//异步执行WebBrowser
public void RunWithSingleThread(object url)
{
ParameterizedThreadStart ps = new ParameterizedThreadStart(GetHtmlWithBrowser);
t = new Thread(ps);
t.IsBackground = true;
t.ApartmentState = ApartmentState.STA;
t.Start(url);
} private WebBrowser wb;
private void GetHtmlWithBrowser(object url)
{
htmlstr = string.Empty; wb = new WebBrowser();
wb.ScriptErrorsSuppressed = true; //防止脚本异常跳出弹窗
wb.ScrollBarsEnabled = true;
wb.Navigate(url.ToString()); wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
while (wb.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents(); //避免假死,若去掉则可能无法触发 DocumentCompleted 事件。
} }
//获取内容
public void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
htmlstr = wb.Document.Body.InnerHtml; }

Webbrowser 在web项目中的使用的更多相关文章

  1. 在基于MVC的Web项目中使用Web API和直接连接两种方式混合式接入

    在我之前介绍的混合式开发框架中,其界面是基于Winform的实现方式,后台使用Web API.WCF服务以及直接连接数据库的几种方式混合式接入,在Web项目中我们也可以采用这种方式实现混合式的接入方式 ...

  2. JAVA WEB项目中各种路径的获取

    JAVA WEB项目中各种路径的获取 标签: java webpath文件路径 2014-02-14 15:04 1746人阅读 评论(0) 收藏 举报  分类: JAVA开发(41)  1.可以在s ...

  3. Web 项目中分享到微博、QQ空间等分享功能

    Web 项目中分享到微博.QQ空间等分享功能 网上有很多的模板以及代码,但是有很多都不能分享内容,简单的测试了下: 以新浪微博为例,文本框中的内容是title属性,下面的链接是url属性,如果你的链接 ...

  4. 【Filter 不登陆无法访问】web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面的功能

    在web项目中写一个过滤器实现用户不登陆,直接给链接,无法进入页面,而重定向到登陆界面的功能. 项目是用springMVC+spring+hibernate实现 (和这个没有多大关系) 第一步: 首先 ...

  5. java web项目中 获取resource路径下的文件路径

    public GetResource{ String path = GetResource.class.getClassLoader().getResource("xx/xx.txt&quo ...

  6. web项目中加入struts2、spring的支持,并整合两者

    Web项目中加入struts2 的支持 在lib下加入strut2的jar包 2. 在web.xml中添加配置 <filter> <filter-name>struts2< ...

  7. Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问

    本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...

  8. 转 web项目中的web.xml元素解析

    转 web项目中的web.xml元素解析 发表于1年前(2014-11-26 15:45)   阅读(497) | 评论(0) 16人收藏此文章, 我要收藏 赞0 上海源创会5月15日与你相约[玫瑰里 ...

  9. web项目中 集合Spring&使用junit4测试Spring

    web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...

随机推荐

  1. python网络爬虫之四简单爬取豆瓣图书项目

    一.爬虫项目一: 豆瓣图书网站图书的爬取: import requests import re content = requests.get("https://book.douban.com ...

  2. VK Cup 2018 - Round 1 A. Primal Sport

    A. Primal Sport time limit per test 1.5 seconds memory limit per test 256 megabytes input standard i ...

  3. matlab学习路线

    知乎:matlab学习路线 MATLAB数学教学视频

  4. 利用autotools工具制作从源代码安装的软件 分类: linux 2014-06-02 23:27 340人阅读 评论(0) 收藏

    编写程序(helloworld.c)并将其放到一个单独目录. helloworld.c: #include<stdio.h> int main() { printf("hello ...

  5. Service官方教程(2)*IntentService与Service示例、onStartCommand()3个返回值的含义。

    1.Creating a Started Service A started service is one that another component starts by calling start ...

  6. ORA-14074: partition bound must collate higher than that of the last partition

    There is a error happen in crotab: CREATE parttion report ORA-14074:ORA-14074: partition bound must ...

  7. C#连接数据库_使用读取配置文件的方式

    using System; using System.Collections.Generic; using System.Configuration; using System.Data.SqlCli ...

  8. Android EventBus3.0详解

    修改日志 -- 添加索引部分得细节,添加kotlin的支持方式 https://www.jianshu.com/p/31e3528ca7e5

  9. 掌握Spark机器学习库-07-最小二乘法

    1)最小化残差平方和 2)原理,推导过程 3)例子

  10. eclipse 升级note

    参考http://www.cnblogs.com/jiqingwu/archive/2013/05/26/eclipse_plugins_import.html. 最终决定采用 启动 eclipse. ...