How Can I Tell Whether a Web Page is Accessible?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I tell whether or not a Web page is accessible?

— JW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JW. To tell you the truth, these are the kinds of questions we hate, questions where we don’t know the answer off the top of our heads but where it seems like there ought to be a way to do this. Sometimes in the world of scripting the hard things turn out to be easy but, conversely, the easy things sometimes turn out to be hard.

Fortunately, this one turned out to be easy. We knew there was a way to retrieve the contents of a Web page; what we didn’t know was whether there was a straightforward way to determine whether those contents were retrievable (that is, whether the Web page was accessible). But here you go. This script will tell you whether the home page for the Script Center is up and running:

On Error Resume Next

strURL = “http://www.microsoft.com/technet/scriptcenter/default.mspx”

Set objHTTP = CreateObject(“MSXML2.XMLHTTP”) objHTTP.Open “GET”, strURL, FALSE objHTTP.Send

Wscript.Echo(objHTTP.statusText)

We begin by assigning the URL for the Script Center to a variable named strURL. Next we create an instance of MSXML2.XMLHTTP, a COM object that provides a way for client computers to interact with HTTP servers. We then call the Open method, passing three parameters: “GET” (which means to retrieve information from the server); strURL, the variable holding the URL to the Script Center home page; and FALSE, to make the call synchronous. After that we use the Send method to send our request to the HTTP server.

So how do we know if http://www.microsoft.com/technet/scriptcenter/default.mspx is accessible or not? Well, we simply check the value of the statusText property. If statusText is OK then the page was accessible; if it’s anything other than OK, then something went wrong and we couldn’t reach the specified URL.

Incidentally, the contents of the Web page are returned as the ResponseText property of the MSXML2.XMLHTTP object. That means you can use the FileSystem object to save the contents of the page to a file. For example:

On Error Resume Next

Const ForWriting = 2

strURL=”http://www.microsoft.com/technet/scriptcenter/default.mspx”

Set objHTTP = CreateObject(“MSXML2.XMLHTTP”) objHTTP.Open “GET”, strURL, FALSE objHTTP.Send

Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set objFile = objFSO.CreateTextFile(“script_center.htm”, ForWriting) objFile.Write objHTTP.ResponseText objFile.Close

Bear in mind that the formatting of the saved document might not be identical to the formatting of the Web page; that depends on such things as the style sheets used by the Web page; whether the page uses relative or absolute links to graphics, etc. In most cases, however, the page will be readable, even if the formatting might be a bit off.

0 comments

Discussion is closed.

Feedback usabilla icon