I need to be able to retrieve data from a website from a Xojo web app. It’s an XML file. The goal is to have it downloaded to the user’s computer. WebHTMLViewer has a loadURL method, but I don’t need to show the site.
Is this possible?
I need to be able to retrieve data from a website from a Xojo web app. It’s an XML file. The goal is to have it downloaded to the user’s computer. WebHTMLViewer has a loadURL method, but I don’t need to show the site.
Is this possible?
look at xojo examples using urlconnection
Here is what I use to get a web file as a FolderItem or as a String:
Protected Function getURLConnectionFolderItem(URLConnectionHost As String, f As FolderItem, URLConnectionTimeOut As Integer = 10) As FolderItem
Var tempURLConnection As New URLConnection
Var MIMEType As String = "application/json"
Var HTTPStatusCode As Integer
If f = Nil Or URLConnectionHost = "" Or (URLConnectionHost.left(7) <> "http://" And URLConnectionHost.left(8) <> "https://") Then
Return Nil
End If
Try 'network timeout causes exception!
#If TargetWindows Or TargetLinux Then
tempURLConnection.AllowCertificateValidation = False 'Windows gets a Security Error if True
#EndIf
tempURLConnection.SendSync("GET", URLConnectionHost, f, URLConnectionTimeOut)
HTTPStatusCode = tempURLConnection.HTTPStatusCode
tempURLConnection.Disconnect
Catch Error
Return Nil
End Try
If f <> Nil And f.Exists Then 'And HTTPStatusCode = 0
Return f
Else
Return Nil
End If
End Function
Protected Function getURLConnectionFolderItemString(URLConnectionHost As String, URLConnectionTimeOut As Integer = 30) As String
Var f As FolderItem = CommonFolders.getDownloadFolderItem("Temp.dat", True, True) 'get a temp FolderItem
f = getURLConnectionFolderItem(URLConnectionHost, f, URLConnectionTimeOut)
If f <> Nil And f.Exists Then
Return CommonFolders.getFileToString(f, True).DefineEncoding(Encodings.UTF8) 'read FolderItem as Binary String
Else
Return ""
End If
End Function
Protected Function getFileToString(f As FolderItem, asBinary As Boolean = False) As String
'converts the contents of a file to a string
'CopyFile creates a read only file, whereas this routine creates a file that can be written to
Var tempTextInputStream As TextInputStream
Var tempBinaryStream As BinaryStream
Var returnString As String
If f = Nil Or Not f.Exists Then
Return "" ' "0"
Else
If asBinary Then
tempBinaryStream = BinaryStream.Open(f)
returnString = tempBinaryStream.Read(tempBinaryStream.Length)
'tempBinaryStream.Flush
tempBinaryStream.Close
Return returnString
Else
tempTextInputStream = TextInputStream.Open(f)
returnString = tempTextInputStream.ReadAll
tempTextInputStream.Close
Return returnString
End If
End If
End Function