Handling Live Web Content in WSH - Part 1

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.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
September 06, 2006
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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.

Processing the Response

We begin processing the html response by setting up an If statement.  Basically, we’re just going to make sure that our variable actually holds some data before we try to play with it.  If it doesn’t, that means that there was an error connecting to the site.

If strHTMLText <> "" Then

   varStart = inStr(1, strHTMLText, "Current IP Address:", _

       vbTextCompare) + 19

   If varStart Then varStop = inStr(varStart, strHTMLText, "</body>", _

       vbTextCompare)

   If varStart And varStop Then strIP = mid(strHTMLText, varStart, _

       varStop - varStart)

Okay, our first line checks to makes sure that the strHTMLText variable is not empty.  If it isn’t, then we begin processing it.  The second line uses the VB inStr() function.  The inStr() function will match our search parameter within a given string and return an integer indicating the starting character where our search criteria appears within that string.  So we look at the source code and pick a unique string that appears before our IP address.  In this case we’ve chosen “Current IP Address:”.  The inStr() function will return the position of the first character is finds in the matching string, so we add 19 (the number of characters in our search string) to be sure that it returns the value just before our IP address.

The ideal way to do this would be to use our IP address as the search string, but since we don’t know what it is, this is a viable workaround.  We could also use regular expressions by using the RegExp object, but that’s a little more difficult, and it also adds quite a bit of extra code.

In the third line we go back to the inStr() function to find the starting character after our IP address.  So again, we check the source code and find a unique search string.  This time inStr() will return the correct value so we don’t need to adjust it.  We’ll assign this to the variable varStop.

Now that we’ve narrowed down the information that we want, our next goal is to remove all of the extra text from our string.  We do this in the fourth line by implementing VB’s mid() function to cut one string from the middle of another.  Essentially, it performs a left trim and a right trim at the same time using the values that we specify.  We assign the results of the mid() function to a string variable strIP.  If all went well strIP should contain only our IP address.

A Little Error-Handling

So you’re probably asking what those extra If statements are for, right?  Those are just to make sure that the VB functions are actually returning usable values.  This prevents our script from crashing if an error occurs.  Ideally, we would add some error handling, but that’s beyond the scope of this tutorial.  More information about error-handling can be found in the Microsoft Windows 2000 Scripting Guide located on Microsoft’s TechNet website.

http://www.microsoft.com/technet/scriptcenter/guide/

Back to our script, we need to finish off our original If statement with the following code.

Else

   strIP = "Unavailable"

End If

getIP = trim(strIP)

This code only executes if strHTMLText is empty, meaning that we didn’t get a response from the website.  We could issue some kind of alert to the user to indicate there was a problem but we’re just going to assign “Unavailable” to strIP instead.  Remember, this is the variable that would hold our IP address had everything worked out correctly.

You now have one of two things.  The variable strIP either contains your IP address or it contains the text “Unavailable”.  In line 4 we make use of VB’s trim() function to make sure that strIP doesn’t contain any leading or trailing spaces. You can do with this information whatever you like.  For our purposes here, we’re just going to end by echoing it back with WSH’s echo method.

Wscript.Echo getIP

Save your work as checkip.vbs and double-click it to see if it works.  If you followed along correctly, you should get an alert box that displays your external IP address.  If the script returns “Unavailable”, make sure you are connected to the internet and that the site isn’t down.

Okay, let’s recap.  We used the XMLHttpRequest object to request a website.  That website was captured into a string variable.  We used VB's inStr() and mid() functions to cut away everything except the text that we wanted, and then we echoed that back to the user.

Go ahead and congratulate yourself.  You’ve just learned how to capture live web content in WSH!

The Final Code

Do you want to use this in other code?  This code snippet makes for a great function as well.  In the last line I used a string variable getIP for a reason. The variable you use here must match the name that you give to your function.  This tells your function to return the value of strIP when it completes.  Next, you enclose the entire snippet with the Function getIP() and End Function statements.  You can now insert this function into any code you like.  Let’s take a look at our complete code as a function.

Function getIP()

   Set objxmlHTTP = _

       CreateObject("Microsoft.XMLHTTP")

   Call objxmlHTTP.Open("GET", "http://checkip.dyndns.org", False)

   objxmlHTTP.Send()

 

   strHTMLText = objxmlHTTP.ResponseText

   Set objxmlHTTP = Nothing

 

   If strHTMLText <> "" Then

       varStart = instr(1, strHTMLText, "Current IP Address:", _

          vbTextCompare) + 19

       If varStart Then varStop = instr(varStart, strHTMLText, _

          "</body>", vbTextCompare)

       If varStart And varStop Then strIP = mid(strHTMLText, varStart, _

          varStop - varStart)

   Else

       strIP = "Unavailable"

   End If

   getIP = trim(strIP)

End Function

Now a call to your function would return your IP address.  Anytime you need it, simply use the following line to assign it to the string variable result.

result = getIP()

It’s that simple.  You now have one very powerful piece of reusable code.  With a little tweaking it could pull any information you like from whatever website you choose.

There are many other ways you can implement this same type of script.  Perhaps you want a script that will grab the local headlines from your favorite news site.  Or maybe you just want to create a script that tests to see if your website is up and running.  There are many reasons you might want to connect to the internet and handle live web content.  Whatever your reason is, you now have the tools to do it.  Until next time, keep coding!

blog comments powered by Disqus
WINDOWS SCRIPTING ARTICLES

- More Windows Scripting Workarounds from Nilpo
- Overloading Methods and More in VBScript
- Improving MFC for Windows Vista
- Regular Expressions in VBScript
- Working with Dates in WMI
- Completing Calendars with VBScript Date Func...
- Building Calendars with VBScript Date Functi...
- Working With Dates and Times in VBScript
- Designing WCF DataContract Classes Using the...
- Understanding Dates and Times in VBScript
- Working With Arrays in VBScript
- Compressed Folders in WSH
- Using .NET Interops in VBScript
- Nilpo`s Scripting Secrets, Vol I
- Database operations using Silverlight 2.0 WC...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials