Quiz #1: Will this code work correctly and why?

Erel

B4X founder
Staff member
Licensed User
Longtime User
This question is about HttpUtils2: HttpUtils2 - Web services are now even simpler

In some cases we need to modify the request. For example we may need to add a header to the message before it is sent.

B4X:
Dim Job As HttpJob
Job.Initialize("Job", Me)
Job.Download("http://www.b4x.com")
' ****** The header is set after Download is called ?!
Job.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)")

Note that it is not possible to call Job.GetRequest before Job.Download as the request is initialized in Job.Download.

Does it work?
Why?
 

keirS

Well-Known Member
Licensed User
Longtime User
Surely that depends on the status of HTTPutils2 service? My guess is that it will not work if the service is active but it could stand a chance of working if the service has to be started.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
It works because CallSubDelayed2 is used to start the service and call a sub. Since CallSubDelayed uses the message queue, the service code/sub will not be executed until the current thread exits, which will exit after SetHeader is set, and whenever the calling sub completes.
EDIT: Ofcourse, I didnt know this before to ever use it. Good technique, but not so straight forward for readability.
 
Upvote 0
Top