Hi
If I use the HTTPRequest for a synchronous request how long will the request wait for a response? I am accessing a REST API that waits for user input and I am concerned that if the user never responds that the request will wait indefinitely and I would like to ensure that wait is no more than 30 seconds.
Thanks,
Simon
HTTPRequest Timeout
HTTPRequest Timeout
Simon White
dCipher Computing
dCipher Computing
Re: HTTPRequest Timeout
Technically, HTTPRequests could wait indefinitely. Timeouts must be manually set, either on the client or server side. For synchronous requests, simply initiate the connection within a thread, and implement a timer to kill the thread at the required timeout. Here's an example; to test the timeout results, substitute the server accordingly.swhite wrote:...HTTPRequest for a synchronous request how long will the request wait for a response? I would like to ensure that wait is no more than 30 seconds.
Code: Select all
InitNetwork()
Procedure httpRequestProc(*arg)
;substitute with an unresponsive server to test the timeout results
HttpRequest = HTTPRequest(#PB_HTTP_Get, "https://www.google.com", "")
If HttpRequest
SetGadgetText(1, "HTTP Request completed.")
FinishHTTP(HTTPRequest)
Else
SetGadgetText(1, "HTTP Request creation failed!")
EndIf
EndProcedure
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, #PB_Ignore, #PB_Ignore, 400, 200, "HTTP Request", wFlags)
TextGadget(0, 20, 20, 360, 40, "Time: 00:00:00", #PB_Text_Right)
TextGadget(1, 20, 80, 360, 40, "HTTP Request...", #PB_Text_Center)
AddWindowTimer(0, 0, 1000)
HTTPRequestTimeout = 30 * 1000 ;30 seconds
httpRequestThreadStart = ElapsedMilliseconds()
httpRequestThread = CreateThread(@httpRequestProc(), 0)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
appQuit = #True
Case #PB_Event_Timer
SetGadgetText(0, "Time: " + FormatDate("%hh:%ii:%ss", Date()))
If IsThread(httpRequestThread) And
ElapsedMilliseconds() - httpRequestThreadStart > HTTPRequestTimeout
KillThread(httpRequestThread)
SetGadgetText(1, "HTTP Request timed out! Thread killed.")
EndIf
EndSelect
Until appQuit
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel
Re: HTTPRequest Timeout
Thanks TI-994A
Having network problems and this helps speed up detection.
Having network problems and this helps speed up detection.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: HTTPRequest Timeout
Also, you can use libcurl throug the excelent libcurl.pbi by infratec
Last edited by Caronte3D on Thu Sep 07, 2023 9:28 pm, edited 1 time in total.
Re: HTTPRequest Timeout
Or simply use:
Available since PureBasic 6.02 beta 3
Code: Select all
HTTPTimeout(ConnectTimeout [, GlobalTimeout])
Re: HTTPRequest Timeout
I missed itinfratec wrote: ↑Thu Sep 07, 2023 9:22 pm Or simply use:Available since PureBasic 6.02 beta 3Code: Select all
HTTPTimeout(ConnectTimeout [, GlobalTimeout])
Re: HTTPRequest Timeout
Haha, I missed it too.
I looked briefly, thinking it was in the request parameters.
Thanks!
I looked briefly, thinking it was in the request parameters.
Thanks!
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum