WebClient 运行 javascript [英] WebClient runs javascript

查看:30
本文介绍了WebClient 运行 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.aspx 页面,其中包含一些控制分页的 JavaScript 函数.

I have one. aspx page that has some JavaScript functions that control paging.

我可以在 WebBrowser1_DocumentCompleted

WebBrowser1.Document.Window.DomWindow.execscript ("somefunction();", "JavaScript")

网络浏览器很慢,我更喜欢使用System.Net.WebClient.DownloadString.

The webbrowser is very slow and I prefer to use System.Net.WebClient.DownloadString.

有什么方法可以使用更快的 System.Net.WebClient 方法或其他方式运行此脚本吗?

Has some way to run this script with the System.Net.WebClient methods that are faster, or some other way?

推荐答案

好吧,不.WebClient 是一个 HTTP 客户端,而不是网络浏览器.

Well, no. WebClient is an HTTP client, not a web browser.

HTTP 客户端遵循 HTTP 规范;您的 HTTP 请求生成 HTML 的事实与客户端无关.

An HTTP client follows the HTTP spec; the fact that your HTTP requests result in HTML is irrelevant to the client.

另一方面,Web 浏览器除了作为 HTTP 客户端之外,还知道如何解析 HTML 响应(以及执行 JavaScript 等).

A web browser, on the other hand, in addition to being an HTTP client, also knows how to parse HTML responses (and execute JavaScript, etc.).

看起来你要找的东西叫做无头浏览器",它支持在 DOM 上加载 HTML 和运行 JavaScript,完全符合你的需要.与普通浏览器相比,无头浏览器通常也相当快,因为​​它们不需要进行任何渲染.

It seems that what you are looking for is called a "headless browser", which supports loading HTML and running JavaScript on the DOM, exactly like you need. Headless browsers are also generally quite fast compared to normal browsers, since they don't need to do any rendering.

有几种无头浏览器.HtmlUnit(可以是 转换 以在 .NET 上运行)似乎是一个不错的选择,envjs(它是用 JavaScript 编写的,可以嵌入到 .NET 中).不幸的是,我对两者都没有经验,但它们看起来都非常酷,尤其是 envjs.更新:一个不错的、更新的无头浏览器列表已在 GitHub 上发布.

There are several headless browsers. HtmlUnit (which can be converted to run on .NET) seems like a good choice, as does envjs (it's written in JavaScript, which can be embedded in .NET). Unfortunately, I have no experience with either, but they both look super-cool, especially envjs. Update: a nice, more up to date list of headless browsers has been published on GitHub.

还有其他 替代品WebBrowser 控件如果您想继续使用控件,可能会也可能不会更快.

There are also other alternatives to the WebBrowser control which may or may not be faster in your case, if you want to stay with a control.

这篇关于WebClient 运行 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆