|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIf you want to access web pages within your .NET code, which means having access to the HTML document object model and evaluation of embedded scripts, the only way to go is to wrap the Internet Explorer COM interface ( The greatest is the inability to capture an Another drawback is the inability to spawn two or more Internet Explorer instances within the same process having different cookie stores. All browsers instantiated in your runtime will share the same resources, therefore you are unable to, e.g., login in as two users at the same time to a cookie based web page. There is a workaround for this (launching via For my purposes, I needed a simple web client, which allows me to access the basic HTML document object model objects and simple JavaScript code (like the evaluation of the default ASP.NET client side validators). PrerequisitesFor building a JavaScript enabled WebClient, we need at least the following three basic components: A HTTP protocol implementation, a HTML document object model, and a JavaScript engine. Since .NET already offers a lot of built-in types for handling at least similar tasks, we will reuse them as much as possible. A HTTP protocol implementationThe types A HTML document object modelCurrently, the .NET framework does not support a HTML object model. But since HTML is very similar to XML, we will use A JavaScript engineThe .NET framework offers a JavaScript implementation called 'JScript.NET'. It is available through the ImplementationCb.Web.WebClientThe type It has two events for handling dialog stuff like alert ( // Basic example
WebClient myWebClient = new WebClient();
myWebClient.OnAlert += new AlertHandler(WebClient_OnAlert);
myWebClient.Get("http://www.thecodeproject.com/");
Cb.Web.Html.HtmlDocumentThe HTML document object model can be, as already mentioned, accessed by using the property myWebClient.Window.Document.GetElementById("Email").
SetAttribute("value", email);
myWebClient.Window.Document.GetElementById("Password").
SetAttribute("value", password);
myWebClient.Window.Document.Forms["subForm"].Submit();
By extending The type Cb.Web.ScriptingFor implementation of our JavaScript engine, I used the The interesting part of this implementation is to hide the .NET signature of our DOM objects and provide the standard DOM level signature, and also to add expando features to all scripting objects. 'Expando' means the ability to attach properties to any object at any given time. To achieve these features, all objects which are populated to our scripting engine inherit from class HtmlDocument : IReflect {
// Note: this example is stripped, its main purpose is to show you
// the idea behind expando objects and property/method resolving
public MemberInfo[] GetMember(string name) {
switch (name) {
case "getElementById": return GetType().GetMember("GetElementById");
}
// ... handle expando properties ...
}
}
Expando properties are implemented by creating a dynamic document.MyVar = "World";
alert("Hello " + document.MyVar);
// Will alert 'Hello World'
Final NotesIn its current state, the attached source code can handle simple web pages and evaluate simple JavaScript sources. It currently has (basically) no CSS support nor any real DOM compliance. Its performance isn't on the edge either, but it works on basic ASP.NET web pages, and can be used in multi threaded environments. The attached demo shows most of its currently implemented features. It opens "www.thecodeproject.com", searches for one of my articles, and if you modify the source code and provide your username and password, will rate it with 5 points ;-). On a side note, I've removed the VersionsVersion 1.0 (released on 26.03.2006)
References
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||