WebClient 运行 javascript
我有一个。 aspx 页面,具有一些控制分页的 JavaScript 函数。
我可以使用 WebBrowser1_DocumentCompleted
中的以下方法通过网络浏览器运行此 javascript 函数。
WebBrowser1.Document.Window.DomWindow.execscript ("somefunction();", "JavaScript")
网络浏览器非常慢,我更喜欢使用 System.Net.WebClient.DownloadString
。
有某种方法可以使用更快的 System.Net.WebClient 方法或其他方式运行此脚本吗?
I have one. aspx page that has some JavaScript functions that control paging.
I can run this javascript function via webbrowser with the following method within the WebBrowser1_DocumentCompleted
WebBrowser1.Document.Window.DomWindow.execscript ("somefunction();", "JavaScript")
The webbrowser is very slow and I prefer to use System.Net.WebClient.DownloadString
.
Has some way to run this script with the System.Net.WebClient
methods that are faster, or some other way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,不。
WebClient
是一个HTTP 客户端,而不是网络浏览器。HTTP 客户端遵循 HTTP 规范;您的 HTTP 请求生成 HTML 的事实与客户端无关。
另一方面,Web 浏览器除了作为 HTTP 客户端之外,还知道如何解析 HTML 响应(并执行 JavaScript 等)。
看来您正在寻找的称为“无头浏览器”,它支持加载 HTML 并在 DOM 上运行 JavaScript,正如您所需要的那样。与普通浏览器相比,无头浏览器通常也相当快,因为它们不需要进行任何渲染。
有几种无头浏览器。 HtmlUnit (可以是 converted to run on .NET)似乎是一个不错的选择,envjs (它是用 JavaScript 编写的,可以嵌入.NET)。不幸的是,我对这两者都没有经验,但它们看起来都超级酷,尤其是 envjs。更新:一个不错的、最新的无头浏览器列表已在 GitHub 上发布。
还有其他 WebBrowser 控件的替代品,它们可能会或可能会如果你想保持控制,就你的情况来说不要更快。
Well, no.
WebClient
is an HTTP client, not a web browser.An HTTP client follows the HTTP spec; the fact that your HTTP requests result in HTML is irrelevant to the client.
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.).
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.
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.
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.