This example was created in response to a question Q_21361792 posted at Experts Exchange. A person wanted to query dozens of PDAs, each of which has a web server and a page with the date and time stamp for when an employee comes to or leaves work. By using this technique, the person could write one web page to interrogate each device and display all of the information as a summary.
The example below reads the
date and time from http://www.rodsdot.com/ee/timedate.htm. The data on the page is a string value of course. We also take the data and convert it to date and time values so we can perform calculations on the values. The XMLHTTP object can be used with server side scripts, such as ASP/PHP/JSP and with client side JavaScript in most browsers (IE/Mozilla/Opera). The use client side depends on the browsers support for the XMLHTTP object and not all older browsers have that support.
08:59:17 Central Daylight Time
03/23/2005
Device date is different by 1265 days.
Device time is different by 1821600 minutes.
<%
' THE CODE
'for each device
Response.Buffer = True
Dim objH, tmpStr, timeStr, dateStr, htmlOut
' Create an xmlhttp object:
Set objH = Server.CreateObject("Microsoft.XMLHTTP")
' Opens the connection to the remote server.
' objH.Open "GET", "http://devicename/page.htm", False
objH.Open "GET", "http://www.rodsdot.com/ee/timedate.htm", False
' Actually Sends the request and returns the data:
objH.Send
' The data from the web page is now in objH.responseText.
' Getting the data we want is nothing fancy. We just parse
' the data string for what we are looking for.
tmpStr = objH.responseText
tmpStr = Mid(tmpStr, InStr(tmpStr, "<b>Time:</b>")+13,100)
timeStr = Left(tmpStr, InStr(tmpStr, "</br><b>Date:</b>")-1)
dateStr = Mid(tmpStr, InStr(tmpStr, "</br><b>Date:</b>")+18,10)
' htmlOut is just holding the output so we can write it
' where we want on our example page.
htmlOut = htmlOut & timeStr & "<br>" & dateStr
' Not related to screen scraping, but specific to the
' original question - we will perform so calculations.
Dim deviceDate, deviceTime, localDate, localTime
deviceDate = CDate(dateStr)
localDate = Date
htmlOut = htmlOut & "<br>Device date is different by " & DateDiff("d", deviceDate, localDate) & " days."
deviceTime = TimeValue(Left(timeStr,8))
localTime = Time
htmlOut = htmlOut & "<br>Device time is different by " & DateDiff("n", deviceDate, localDate) & " minutes."
' clean up
Set objH = Nothing
'next
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="author" content="Roderick Divilbiss">
<meta name="copyright" content="? 2005 Roderick Divilbiss">
<title>Screen Scrape Using XMLHTTP</title>
</head>
</head>
<body>
<%=htmlOut%>
</body>
</html>