VB6 - WebSpring Class: Read, Write, Publish to Web from your VB6 Program
There are times when you need your program to obtain, create, or update remote files. Within a LAN this is normally no problem and you can just use a regular file share. But what if your program is being used across the Internet? Now what?
Or maybe you're creating the latest and greatest Web site development tool. But you need a way for your program to "publish" the pages and images to the user's Web server?
Sometimes the answer might be FTP. This seems to be the default action of most VB programmers anyway. Yet FTP has a number of limitations, including a firewall-hostile protocol that exchanges logons and data in plain text.
Is there another answer?
The majority of Web servers support (or can support) two common "Read/Write Web" protocols that work as extensions to common HTTP (or HTTPS for security).
The older of these is known as FrontPage Server Extensions (FPSE) and is actually very common. The other is a little newer and in some ways more versatile. It is called Web Distributed Authoring and Versioning (WebDAV, or DAV) and is not available with all cheap Web hosting plans. Both IIS (even through IIS 7) and Apache can work with either FPSE or WebDAV though, once you install the options and enable them.
While hardly the same as a local file share, you get a lot of capability from FPSE/DAV without a lot of trouble. The limitation is that you must work with the files as data streams, i.e. like a sequential file. No random I/O, no using it to host something like a Jet MDB database. Think "open and save."
How does this help the VB programmer?
While unknown to a lot of people, ADO was designed to handle a lot more than just database access.
Beginning with MDAC 2.5 ADO gained a lot of capabilities. Included in these is the ability to work with the new Internet Publishing Provider (MSDAIPP.DSO) as well as various XML and other Data Source Objects (DSOs).
This means you can use ADO to read, write, download, and upload data from and to your Web site. Anywhere on the Internet.
WebSpring
To make this a bit easier to use I created a wrapper class that I call WebSpring.
A small joke there: you work with the Web files via ADODB.Stream objects, and streams flow from a spring... get it? Ok, a very small joke.
WebSpring comes with two auxiliary classes. One of these is WSDirItem which contains a file's attributes from a directory request. The other is WSDirItems which is the collection of WSDirItem objects that the directory request returns to you. There is a third one of course: the ADODB.Stream class.
WebSpring offers several Methods:
Connect()
Delete()
DirItems()
Disconnect()
DownloadFile()
Exists()
MakeDir()
OpenFile()
UploadFile()
There is much more detail on these methods and on WebSpring's Properties in comments at the head of the code.
WebSpring does not attempt to expose all of the functionality available. However it does provide quite a lot, and you have the source code if you want to extend it.
When you look at my code you'll find most of it shockingly simple. Go ahead, make my day: code directly against ADO yourself. And what has been stopping you all these (nine) years then?
So what does WebSpring buy me?
WebSpring gives you an easy to use way to add Web site publishing to a VB6 program.
More than this, it allows you to use a FPSE Web site or a WebDAV host as a read/write central data respository. Think of it as a basic version of the "Cloud Computing" we're beginning to read so much about.
If you have a WebDAV site, you might set up a client account and embed its credentials (User/PW) in your program. Then you can give that User read-only access to one folder and write-only access to another. This could be used to check for and download new program versions for autoupdating or new data files. It could be used for submitting registration info, crash reports, feedback form data, etc.
Pretty much anything you've been resorting to FTP for, except that it is:
Easy to use,
Firewall friendly,
Secure,
Can read to and write from memory as well as disk!
What special knowledge should I have to apply WebSpring?
It would be very helpful to read up on the ADODB.Stream object. When you use the WebSpring.OpenFile() method the results are returned to you as an open Stream object.
As you study ADODB.Stream you'll find the ways you can use it to process binary data or text data. It can even handle conversions to or from Unicode, ASCII, UTF-8, etc. as well as alternate line delimiters (LF, CR, or CRLF).
Just like opening a text file sequentially with VB's Open statement, you can't just "insert and delete" data. But in much the same way you can append or replace data. Text can be read or written by line or by a number of characters. Binary data can be read in chunks or all at once. You can move the "cursor" much like using a VB Seek statement.
WSDemo
The attached archive contains the WebSpring classes and a sample project that will excercise most of WebSpring's capabilities.
To use it you will need a Web site that is either FrontPage or WebDAV enabled. Surprisingly many already do support one or the other. In lieu of this you could run IIS or Apache with the proper extensions installed and enabled.
Then you'll need the URL of the site and a User/PW that has full access to your site. For many simple Web sites this is the same User/PW account you use with FTP to update the site.
Beyond file I/O
Another thing that can be done with a FPSE-enabled site is to pass parameter values to WebSpring.OpenFile() that cause it to do an HTTP GET request. This can be used with a scripted Web page to actually pass GET parameters to and run the script, returning the script output in the Stream you get back.
This is documented to work for ASP pages with IIS. I can think of no reason why it shouldn't work as well for an Apache/PHP page.
This would give you even more "Cloud" functionality. Not just file I/O but also the ability to inquire/update a database at the server!
Cautions
Many of the free Web hosting providers do not like you to make exclusive use of a free Web site for this purpose. Some want to add banner ads and such to your Web pages. others have no banners but their Terms of Service prohibit use as a data repository.
You might have to:
Self-host on a server of your own (simple enough to do, and only a simple firewall rule or port mapping requierd),
Use the webspace provided in your ISP account (unless it also prohibits data-only use), or
Find a pay provider offering either WebDAV hosting or more permissive FPSE-enabled Web hosting.