Lotusscript Code – HTML retrieval class

I saw a question in the DeveloperWorks forum about retrieving a web page (in this particular case in order to get some data out of it), and realized that I never posted my HTML retrieval class…

So without further ado, here it is. It should be fairly self-documenting… Create a new object, then call the GetHTTP method with a URL to get a string representing the HTML code of that URL.  This is Windows only, by the way.

Class RemoteHTML
  Private httpObject As Variant
  Public httpStatus As Integer
 
  Public Sub New()
    Set httpObject = CreateObject("MSXML2.ServerXMLHTTP")
  End Sub

  Public Function GetHTTP(httpURL As String) As String
    Dim retries As Integer
    retries = 0 
    Do
      If retries>1 Then
        Sleep 1   ' After the two first calls, introduce a 1 second delay betwen each call
      End If
      retries = retries + 1
      Call httpObject.open("GET", httpURL, False)
      Call httpObject.send()
      httpStatus = httpObject.Status
      If retries >= 10 Then
        httpStatus = 0     ' Timeout
      End If
    Loop Until httpStatus = 200 Or httpStatus > 500 Or httpStatus = 404 Or httpStatus = 0
    If httpStatus = 200 Then
      GetHTTP = Left$(httpObject.responseText,16000)
    Else
      GetHTTP = ""
    End If
  End Function

End Class

Leave a Reply