|
Microsoft.XmlHttp Object
You instantiate this COM object from a
script. For instance, say you want to download an html file data you can adapt
this code:
'Creates an instance of Microsoft.XmlHttp
Set http = CreateObject("Microsoft.XmlHttp")
'Disables error prompts
On Error Resume Next
'Sets the URL to download
url = "http://www.mywebsite.com/mywebpage.html
http.Open "GET", url, False
'Receives data
http.send ""
sWebpage = http.responseText
This is a very simple yet effective way to download data from the internet.
Possibly the only limitation is that the method is synchronous, that is, it will
block the script execution until the data finished downloading. The interface
could hang until the script finished its job. Still, this can be fast and
reasonable for most cases.
Please refer MSDN help on how to use XMLHTTP object.
A more complex method of accessing internet data is to use the Microsoft Web
Browser.
Microsoft Web Browser Control
You can create an hidden MS Web Browser ActiveX control to download internet
data. This object is very powerful and can be used for different purposes than
the actual html rendering.
Refer MSDN references on this object for information on how to access various
html elements, modify the presentation, properties, etc.
As a quick start, create a blank object. Then add a new script. In the menu
select "ActiveX control...". Scroll down and select "Microsoft Web Browser".
You can use this simple method to navigate to a page:
Control.navigate2 "http://www.mywebsite.com/mywebpage.html"
Select "Event Wizard.." in the script editor menu and add this event:
'Fired when the document being navigated to reaches ReadyState_Complete.
Sub Control_DocumentComplete(pDisp, URL)
End sub
This function is called when the page finished loading. The mechanism is
asynchronous, so Object.Navigate2 won't block the script execution flow.
When the document is complete you can use the control methods to access the page
data. The simpliest one is:
Control.document.body.innerHTML
It will return the whole page content.
Passing URL Parameters
When you are passing URL parameters, no matter what method you choose, make sure
to correctly escape the text parameters if they can contain special characters.
The VBScript method to do this is:
escape(string)
Design Guidelines
Widgets and themes that access the Internet are the most difficult to write
properly so they deserve particular attention. You just can’t assume that a user
is connected to the Internet, so browsing could fail and cause very annoying
script errors. You should ALWAYS check the connection status, and also check
about the success of browsing operations. You should assume that you can
retrieve NULL or invalid data.
Also, objects that connect to the Internet should follow a specific usage
protocol to avoid annoyances. They should keep an internal connection status
that just informs the script about “Can I try to access the Internet? YES/NO”.
This is different from the actual connection status, in fact it is more similar
to the Internet Explorer Online/Offline status, that’s independent from the
actual network connection status.
A tentative protocol works like this:
-
Have a global script variable, e.g.
IsConnected.
-
When IsConnected is changed to TRUE,
the object can update and retrieve info from the Internet. It can also start
a timer to periodically do that.
-
When IsConnected is changed to FALSE,
the object won’t try to retrieve info from the internet. Any timer that does
that is “killed”.
-
IsConnected can be first set on
OnScriptEnter by checking for the actual connection status by calling
System.InternetConnected and setting IsConnected accordingly.
-
When IsConnected is FALSE, the
objects GUI should represent this status. I.e. you can use a text object
that shows a red “Offline” text. From here on, only the user can manually
retry to set IsConnected to TRUE (i.e. by clicking another DX object). Here
is another System.InternetConnected check and IsConnected is set
accordingly.
-
The user can manually set the Offline
status as well. I.e. one could just not want an object to keep updating from
the Internet.
|