Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebClient runs javascript

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?

like image 893
Andre Reese Avatar asked Mar 20 '11 23:03

Andre Reese


1 Answers

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.

like image 86
Cameron Avatar answered Sep 24 '22 15:09

Cameron