Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / Visual Basic
Article

WebResourceProvider VB.NET style

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
30 Aug 20023 min read 82.4K   262   24   8
Ravi Bhavnani's WebResourceProvider ported to the .NET Framework

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:

  1. Start building any VB.NET project in Visual Studio .NET
  2. Add WebResourceProvider.vb to your project
  3. Create a new class which inherits from WebResourceProvider
  4. Override the ConstructUrl() method in your derived class. This method specifies the URL to be downloaded.
  5. 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.
  6. Optionally override other WebResourceProvider virtual methods. See the source code of the sample resource providers included in the demo project for examples.
  7. 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)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThanks Pin
elchalateco8-Jul-08 6:10
elchalateco8-Jul-08 6:10 
Thanks Cello,
I was able to use your code [The translation form] by changing couple of lines. This is great!

Thanks very much.

El Chalateco
GeneralStock up or down Pin
Michael J. Collins24-Mar-04 4:59
professionalMichael J. Collins24-Mar-04 4:59 
Generalstrange characters Pin
unruledboy15-Sep-03 16:56
unruledboy15-Sep-03 16:56 
GeneralRe: strange characters Pin
Cello4-Nov-03 9:51
Cello4-Nov-03 9:51 
Questionhow to use it in vb6.0 Pin
prafulla17-May-03 6:48
prafulla17-May-03 6:48 
AnswerRe: how to use it in vb6.0 Pin
Cello17-May-03 7:03
Cello17-May-03 7:03 
QuestionHow to download a webpage Pin
Anonymous21-Aug-02 23:17
Anonymous21-Aug-02 23:17 
AnswerRe: How to download a webpage Pin
Cello26-Aug-02 20:17
Cello26-Aug-02 20:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.