HTTPRequest Timeout - How to abort the request?
-
- User
- Posts: 12
- Joined: Wed Apr 13, 2022 10:08 pm
- Location: Low Orbit
HTTPRequest Timeout - How to abort the request?
Hi,
I am struggling to figure out how to abort an unsuccessful HTTPRequest, more specifically one that should time out. To be clear, it's just a simple GET request to grab the HTML from a website.
For example if I try and run an HTTP GET request on the website test.com, it freezes my program.
I made a function that runs a timer that uses AbortHTTP and FinishHTTP but it still freezes my program.
If anyone can offer any help I would very much appreciate it.
Thank you!
I am struggling to figure out how to abort an unsuccessful HTTPRequest, more specifically one that should time out. To be clear, it's just a simple GET request to grab the HTML from a website.
For example if I try and run an HTTP GET request on the website test.com, it freezes my program.
I made a function that runs a timer that uses AbortHTTP and FinishHTTP but it still freezes my program.
If anyone can offer any help I would very much appreciate it.
Thank you!
Re: HTTPRequest Timeout - How to abort the request?
I am struggling to figure out how to abort an unsuccessful HTTPRequest, more specifically one that should time out.
Code: Select all
; HTTP_Abort
; Marc56us - 2022/08/20
; Based on sample in PB help (HTTPProgress())
; https://www.purebasic.com/documentation/http/httpprogress.html
; URL$ = "http://www.purebasic.com/download/OgreAssimpConverter.zip"
URL$ = "http://test.com/void.html"
Download = HTTPRequest(#PB_HTTP_Get, URL$, "", #PB_HTTP_Asynchronous)
If Download
Repeat
Progress = HTTPProgress(Download)
Select Progress
Case #PB_HTTP_Success
Debug "Download finished"
End
Case #PB_HTTP_Failed
Debug "Download failed"
FinishHTTP(Download)
End
Case #PB_HTTP_Aborted
Debug "Download aborted"
FinishHTTP(Download)
End
Default
Debug "Loaded: " + Progress
; Abort after 5 sec if total still = 0
Count + 1
Total + Progress
If Count = 10 And Total = 0
Debug "Sorry Nothing to Load :-/"
FinishHTTP(Download)
End
EndIf
EndSelect
Delay(500) ; Don't stole the whole CPU
ForEver
Else
Debug "Download error"
EndIf
(I hope the team will put back one of the tested timoute functions in betas.)
Why not use ReceiveHTTPFile() ? A single line would be enough.it's just a simple GET request to grab the HTML from a websit
PS. In my tests, HTTPRequest and ReceiveHTTPFile abort after 21 sec.
-
- User
- Posts: 12
- Joined: Wed Apr 13, 2022 10:08 pm
- Location: Low Orbit
Re: HTTPRequest Timeout - How to abort the request?
This is exactly what I needed, thank you!!Marc56us wrote: ↑Sat Aug 20, 2022 9:43 amI am struggling to figure out how to abort an unsuccessful HTTPRequest, more specifically one that should time out.Quickly created program, you can reduce.Code: Select all
; HTTP_Abort ; Marc56us - 2022/08/20 ; Based on sample in PB help (HTTPProgress()) ; https://www.purebasic.com/documentation/http/httpprogress.html ; URL$ = "http://www.purebasic.com/download/OgreAssimpConverter.zip" URL$ = "http://test.com/void.html" Download = HTTPRequest(#PB_HTTP_Get, URL$, "", #PB_HTTP_Asynchronous) If Download Repeat Progress = HTTPProgress(Download) Select Progress Case #PB_HTTP_Success Debug "Download finished" End Case #PB_HTTP_Failed Debug "Download failed" FinishHTTP(Download) End Case #PB_HTTP_Aborted Debug "Download aborted" FinishHTTP(Download) End Default Debug "Loaded: " + Progress ; Abort after 5 sec if total still = 0 Count + 1 Total + Progress If Count = 10 And Total = 0 Debug "Sorry Nothing to Load :-/" FinishHTTP(Download) End EndIf EndSelect Delay(500) ; Don't stole the whole CPU ForEver Else Debug "Download error" EndIf
(I hope the team will put back one of the tested timoute functions in betas.)
Why not use ReceiveHTTPFile() ? A single line would be enough.it's just a simple GET request to grab the HTML from a websit
PS. In my tests, HTTPRequest and ReceiveHTTPFile abort after 21 sec.
To be honest, I didn't realize I could just download the webpage with ReceiveHTTPFile(). After looking at it I guess the only problem would be if I knew the name of a website but didn't know the name of the homepage and it's extension? I'm building a text editor and wanted to have an 'Open URL' feature where it just loads the HTML straight into the editor but I wanted to have it where the user could just enter the general name of a website and grab the code.
Thank you again!
Re: HTTPRequest Timeout - How to abort the request?
No need to know in advance the name of the root filename (homepage)After looking at it I guess the only problem would be if I knew the name of a website but didn't know the name of the homepage and it's extension?
ReceiveHTTPFile() will load the default root file (who was set by server)
Set savename as you want.
Code: Select all
If ReceiveHTTPFile("http://www.purebasic.com/",
GetTemporaryDirectory() + "Source.txt")
Debug "Source loaded"
Else
Debug "Failed"
EndIf
Re: HTTPRequest Timeout - How to abort the request?
Simply use libcurl directly and set a timeout of 1 or 2 seconds.
-
- User
- Posts: 12
- Joined: Wed Apr 13, 2022 10:08 pm
- Location: Low Orbit
Re: HTTPRequest Timeout - How to abort the request?
Very cool to know. Thanks Marc56us!Marc56us wrote: ↑Sun Aug 21, 2022 9:08 amNo need to know in advance the name of the root filename (homepage)After looking at it I guess the only problem would be if I knew the name of a website but didn't know the name of the homepage and it's extension?
ReceiveHTTPFile() will load the default root file (who was set by server)
Set savename as you want.
Code: Select all
If ReceiveHTTPFile("http://www.purebasic.com/", GetTemporaryDirectory() + "Source.txt") Debug "Source loaded" Else Debug "Failed" EndIf
-
- User
- Posts: 12
- Joined: Wed Apr 13, 2022 10:08 pm
- Location: Low Orbit
Re: HTTPRequest Timeout - How to abort the request?
No.
PB uses libcurl also internally, so all functions are included.
Here are my pbi files:
viewtopic.php?p=591235
The latest file includes also HTTPRequestI() which has a timeout parameter.
PB uses libcurl also internally, so all functions are included.
Here are my pbi files:
viewtopic.php?p=591235
The latest file includes also HTTPRequestI() which has a timeout parameter.
Last edited by infratec on Sun Nov 13, 2022 4:12 pm, edited 2 times in total.
Re: HTTPRequest Timeout - How to abort the request?
Timeout was removed from PB 6 LTS
PureBasic 6.00 released
curl is native on Linux and Mac os, but not in Windows. (but easy to download)
So others solutions need external .pbi and /or DLL
Code: Select all
Beta 9 is available, with some more some improvements:
- Added: Timeout options for HttpRequest commands
Beta 10 is available, with some more some improvements:
- reverted the changes from beta 9 about timeout
curl is native on Linux and Mac os, but not in Windows. (but easy to download)
So others solutions need external .pbi and /or DLL
Re: HTTPRequest Timeout - How to abort the request?
No external dll is needed (only needed if you run a windows server which can not speak TLS 1.3, or if you want features which are not included in the PB version of libcurl)
A libcurl.lib is already in PB included and used by the internal HTTP commands.
My pbi makes them usable in your normal PB code.
The other pbi file includes replacements for the PB commands with timeout parameter.
A libcurl.lib is already in PB included and used by the internal HTTP commands.
My pbi makes them usable in your normal PB code.
The other pbi file includes replacements for the PB commands with timeout parameter.