Handling Live Web Content in WSH - Part 1
(Page 1 of 4 )
From time to time it’s nice to be able to handle live web content with WSH. Maybe you want to capture some text from an HTML page into a text document. Or perhaps you want to check for a program update. In any case, being able to handle live web content can make for a very powerful feature.
So let’s take a real world example. One of the hardest things to do when scripting is determining your external, or WAN, IP address. For those who don’t know what I’m talking about, when you connect to the internet through a local network, your local IP address is hidden and your network displays a public IP address for the rest of the world to see. In other words, your computer has a local IP address that only your network knows. Your network is visible to the world as a single public address so no one can see the actual computers on it.
So why is determining your external address so hard? Well, quite simply, Windows doesn’t provide a way to do it. Since your external address is only visible to the public, you need something outside of your network to tell you what it is. There are several programs available for this, but for our purposes we’re going to use a cool website. The Current IP Address page provided by DynDns.org is a great way to do this. Go ahead and type this address in your browser now and see what it tells you.
http://checkip.dyndns.org
You should get a display similar to the following showing your external IP address:
Current IP Address: xx.xxx.xxx.xx
This is your public IP address. So, how can we get that into our WSH script? To begin, let’s set the framework for our script. We’re going to call it checkip.vbs. The first thing that we need to do is establish a connection to the internet. We do this by connecting to Microsoft’s XMLHttpRequest object. This object is used for making HTTP requests.
Set objxmlHTTP = CreateObject("Microsoft.XMLHTTP")
Next, we use its open method to query our website. The first parameter indicates what HTTP method we want to use. The second is the URL that we want to request.
Call objxmlHTTP.Open("GET", "http://checkip.dyndns.org", False)
Once we’re connected, we use the Send() method to initiate a response from the website.
objxmlHTTP.Send()
The website responds by sending us an html document which we capture in a string variable using the XMLHttpRequest object’s ResponseText property.
strHTMLText = objxmlHTTP.ResponseText
What we have now is the entire html source of the webpage captured as a single string. We must now break that string apart into usable pieces. We want to strip away all of the html markup and other text so that all we have left is the IP address that it displays. If you still have the DynDns page open in your browser, right-click the page and choose View Source and you’ll see what I mean.
Next: Processing the Response >>
More Windows Scripting Articles
More By Nilpo