Introduction
First of all I have to thank Ravi Bhavnani for his excellent WebResourceProvider
article.
I just took the opportunity to translate the code into VB.NET and tried to
use as much .NET Framework functionality as possible.
Please refer to Ravi Bhavnani's article for more in-depth information about
the idea of WebResourceProviders.
The source code provided with this article is a Visual Studio .NET project
with the base class WebResourceProvider
and a demo application
including two derived classes:
- stock quotes
- the translation of a piece of text (which uses also a POST request)
How it Works
You use WebResourceProvider
by deriving your own resource
provider class from it, and overriding any of these virtual methods:
Init
ConstructUrl
IsPost()
GetPostData()
ParseContent()
MoreAvailable()
WebResourceProvider
provides an assortment of methods to help
parse downloaded content. They are:
Method | | Purpose |
At | | Checks whether current location is at a string |
AtExact | | Case sensitive version of at() |
SkipTo | | Advances current location to next occurence of a
string |
SkipToExact | | Case sensitive version of SkipTo() |
SkipBackTo | | Advances current location to previous occurence of a string |
SkipBackToExact | | Case sensitive version of SkipBackTo() |
ExtractTo | | Extracts text from current location to the start of a
string |
ExtractToExact | | Case sensitive version of ExtractTo() |
ExtractToEnd | | Extracts text from current location to end of content |
GetIndex | | Returns current location |
GetLinks | | Returns HREF and IMG links in content |
ResetIndex | | Sets current location to start of content |
ResetIndex | | Sets current location to start of content |
StoreIndex | | Stores current location |
RestoreIndex | | Restores the prevoiusly stored location |
RemoveComments | | Removes comments from content |
RemoveScripts | | Removes scripts from content |
RemoveEnclosingAnchorTag | | Removes anchor tag enclosing a string |
RemoveEnclosingQuotes | | Removes quotes enclosing a string |
RemoveHtml | | Removes HTML from a string |
Trim | | Removes leading and trailing whitespace from a
string |
ReplaceSpecialChars | | Replace special HTML characters like ampersand, umlaut, etc. to its character representation |
Sample Resource Providers
There are two sample resource providers in the source code:
The QuoteProvider
class works by posting a request to Yahoo's basic stock quote
form and parsing the returned information.
The TranslationProvider
class works by posting a request to Google's
translation engine and parsing the returned information. The request
includes the translation mode. The sample performs a reverse translation and
presents it along with the original text for comparison purposes.
Using WebResourceProvider
To use WebResourceProvider
do the following:
- Start building any VB.NET project in Visual Studio .NET
- Add
WebResourceProvider.vb
to your project
- Create a new class which inherits from
WebResourceProvider
- Override the
ConstructUrl()
method in your derived class.
This method specifies the URL to be downloaded.
- Override the
ParseContent()
method in your derived class.
This method extracts information from the downloaded content and stores it in
the derived class' member variables.
- Optionally override other
WebResourceProvider
virtual
methods. See the source code of the sample resource providers included in the
demo project for examples.
- Run and debug your code from within Visual Studio .NET
Acknowledgement
WebResourceProvider
uses the following code written by
others:
Revision History
- 11 June 2002 Initial version of article.
- 27 August 2002 Some bugs fixed, added functions (SkipBackTo, SkipBackToExact, StoreIndex, RestoreIndex, ReplaceSpecialChars)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.