URLConnection - reception of content delayed

i have a class setup with URLConnection Class and it’s event “Content Received”
i call the send command from a custom method which is called by another method, which parses the downloaded data.
Problem: the data does not download or i cannot process the data until the routine ( button click) has completely finished. the data does eventually download, so i know the connection is good, the url is good. it is just timing. i would like to be able to GET the data, and process the data , all in one go.

i have the same exact issue with HttpSocket Class
URLConnection also has a xxxxx.SendSync(“GET”, url, 30) which does not require
a super class, but i still have the same exact issue.

Here is the class:

Dim CustomeURLConnection As new URLConnection


ContentReceived: MainWin.SaveText.Text = Content

Call to the class:
my method: MainWin.DownloadHTTP

Dim myConnection As new CustomURLConnection
myConnection.Send(“GET”, url, 30)

my method: SomeMethod

// DownLoad Data

// Parse URL Content
if MainWin.SaveText.Text <> “” Then
Arr = Split(MainWin.SaveText.Text, “|”)
Amt = Val(Arr(1))
Pamt = Val(Arr(2))
end if

Note: SaveText.text remains “” until the process has completed, this means i cannot parse the data.

Please advise.

There really isn’t any way to count on a tight Request/Receive loop with a URLConnection, so you need to code your solution accordingly. Once you .Send a request you should create another process to monitor the .ContentReceived.

For example:

1.- Create a timer or thread that monitors a variable, maybe call it “MyDataReceived”.
2.- Set the .ContentReceived of your URLConnection to append the data it receives to this variable.
3.- Use the timer to monitor the content of MyDataReceived to evaluate if it has the compete and expected payload.
4.- In the meantime, prevent sending another request until either you have all the content, or until a timeout or other error condition arises.
5.- If all goes well then the method in your timer should move MyDataReceived to be processed, clear its content for the next transaction, and start allowing new requests to process from URLConnection.

My connection would need to be a property of your window, otherwise when your method ends, my connection would go out of scope and would probably be disposed of before the download even starts.

Actually URLConnection is a special class that stays in memory until ContentReceived or Error events fire when it goes out of scope.

It is easier of course to drag an instance of URLConnection to the Window to expose the events directly.

setting up myConnection As UrlConnection in the window’s properties gets an error during compile.

Screen Shot 2023-11-11 at 10.40.12 PM
Screen Shot 2023-11-11 at 10.40.37 PM

Errors result during compile time, when draging URLconnection to the window.

Screen Shot 2023-11-11 at 11.23.39 PM

ok found the solution there are 2 ways

a) rtn = myConnection.SendSync(“GET”, url, 30)
SendSync will suspend the app until the content has been downloaded, accessable from the Event - ContentRecieved

b) rtn = myConnection.Send(“GET”, url, 30)
upon the Event receiving data = open a new window and save contents to a textfield, then a button that parses the data in that same new window
use the Send method and a thread or timer to monitor it’s status.

sendSync is cleaner and less clutter but both ways work


You shouldn’t use SendSync unless you absolutely have to.
URLConnection.SendSync Is a Performance Nightmare, and What You Can Do About It

1 Like

In my case SendSync make sense as i am loading only very small amount of data ( pretty fast ) not much delay. and this task is only done a few times per week at the most. and even if it was a few times a day not much harm there.
however ,
but on the other hand. I would like to know , for posterity how could one accomplish this using a thread ?? got any code ideas …