'Handler class
Sub Class_Globals
Private allSucceeded As Boolean
End Sub
Public Sub Initialize
End Sub
Sub Handle(req As ServletRequest, resp As ServletResponse)
doRequests ' Let another sub handle all the wait for's
StartMessageLoop ' Only one startmessageloop necessary
If Not(allSucceeded) Then
resp.SendError(500, "error....")
End If
End Sub
Sub doRequests
'Process your wait for's here. As structured, they will occur sequentially
Try
Wait For (request1) complete (result1 As String)
'Do the work according to the results of request 1
If result1="XXXXXX" Then
wait for (request2) complete (result2 As String)
If result2 <> "" Then
allSucceeded = True
End If
Else
Dim data As String = "some data to send"
wait for (request3(data)) complete (result3 As String)
If result3 = "" Then
allSucceeded = True
End If
End If
Catch
Log($"Encountered error processing doRequests: ${LastException}"$)
End Try
StopMessageLoop ' as structured, only one call to stopmessageloop required
End Sub
'For request1, the returned string will trigger different outcomes
Sub request1()As ResumableSub
Dim result As String
Dim hp As HttpJob
hp.Initialize("",Me)
hp.Download("http://www.example.com")
Wait For(hp)JobDone(hp As HttpJob)
'I need to return the result here
If hp.Success Then
result = hp.GetString
Else
Log($"Encountered error in request1(): ${hp.ErrorMessage}"$)
End If
hp.Release
Return result
End Sub
'For request2, a return of a blank string = failure
Sub request2() As ResumableSub
Dim result As String
Dim hp As HttpJob
hp.Initialize("",Me)
hp.Download("http://www.example.com")
Wait For(hp)JobDone(hp As HttpJob)
'I need to return the result here
If hp.Success Then
result = hp.GetString
Else
Log($"Encountered error in request2(): ${hp.ErrorMessage}"$)
End If
hp.Release
Return result
End Sub
'For request3, a return of a blank string = success
'Note the usage of SetContentType
Sub request3(data As String) As ResumableSub
Dim result As String
Dim hp As HttpJob
hp.Initialize("",Me)
hp.PostString("http://www.example.com",data)
'hp.GetRequest.SetHeader("Content-Type","application/json")
hp.GetRequest.SetContentType("application/json") ' You're supposed to use the method, not SetHeader, for content type
hp.GetRequest.SetHeader("Content-Length",data.Length)
Wait For(hp)JobDone(hp As HttpJob)
'I need to return the result here
If Not(hp.Success) Then
result = $"Encountered error in request3(): ${hp.ErrorMessage}"$
Log(result)
End If
hp.Release
Return result
End Sub