DesktopX: XMLHTTP Script Segments

From WinCustomize Wiki

Jump to: navigation, search
XMLHTTP Script Segments
Original Author:CerebroJD
Date Created:August 22, 2006
Application:DesktopX
Programs Used:DesktopX

Contents

[edit] Introduction to XmlHTTP

The XmlHTTP object is a control that can be used to send and retrieve data to and from a webpage. Here are a couple different implementations of the object!

[edit] Basic Page Source Retrieval

address = "your address here"
Dim xmlhttp
Set xmlhttp = CreateObject("Microsoft.XmlHttp")
xmlhttp.Open "GET",address,False ' Note the use of the 'address' variable here.
xmlhttp.send ""
PageSource = xmlhttp.responseText

[edit] Sending Data to a Web Form

address = "form submission address"
' Arguments seperated by ampersands.
arguments = "argument1=this&argument2=that&morearguments=continued"
Dim xmlhttp
Set xmlhttp = CreateObject("Microsoft.XmlHttp")
' Note the use of the 'address' variable here, and the POST method!
xmlhttp.Open "POST",address,False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
' Sends the arguments variable, which will be parsed by the form engine as POST data.
xmlhttp.send arguments
PageSource = xmlhttp.responseText

[edit] Search for a string in page source

The following snippet is an example of searching a webpage source for a string using an XMLHttp object.

Sub search_webpage
 ' Load the webpage into a XMLHttp Object 
 url_s = "webpage to search"
 Set xmlhttp = CreateObject("Microsoft.XmlHttp")
 xmlhttp.Open "GET", url_s, False
 xmlhttp.Send

 ' Search the loaded page
 strSearchThis = xmlhttp.responseText
 If instr(strSearchThis, "string to search for") <> 0 Then
  'string is found 
 End If
				
 Set xmlhttp = nothing
End Sub

[edit] Making Sure You Have The Latest Data

The Microsoft.XMLHttp object uses the same cache as internet explorer. Because of that, multiple requests for the same page will often result in retrieving the cached page, instead of the latest version available on the web server. (unless the page has an embedded expiration date/time)

A fairly reliable way to overcome this is to append a query string even though one is not required. For example instead of using this URL:
http://www.foo.com/myfeed.xml
Use this one:
http://www.foo.com/myfeed.xml?ForceUpdate=123456

This results in the XMLHttp object seeing it as a different URL, and retrieving the newest version. This should work for most dynamic server pages such as ASP and XML. It may have limited success for other page types such as HTML or .txt files.

In order to make sure this works every time, the URL query string must be randomized every time. Here is some code to generate a random query string:

address = "http://www.foo.com/myfeed.xml"
'Set the range of the random number
MinNum = 0
MaxNum = 10000
'The Randomize command is needed because of how the vbscript random generator works.
Randomize
MyRandomNumber = Int((MaxNum-MinNum+1)* Rnd + MinNum)
address = address & "?ForceUpdate=" & MyRandomNumber
Dim xmlhttp
Set xmlhttp = CreateObject("Microsoft.XmlHttp")
xmlhttp.Open "GET",address,False ' Note the use of the 'address' variable here.
xmlhttp.send ""
PageSource = xmlhttp.responseText

[edit] Comments/Suggestions

As of DesktopX 3.2 you can use System.DownloadFile(remoteUrl, localPath, bAsync) and System.SendRequest(remoteUrl, postParams, bAsync) to perform the same operations as described earlier. These integrated functions should behave more friendly with some virus scanners that might otherwise complain.