Page 1 of 1

HTTPRequest Timeout - How to abort the request?

Posted: Sat Aug 20, 2022 6:23 am
by phaselock.studio
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!

Re: HTTPRequest Timeout - How to abort the request?

Posted: Sat Aug 20, 2022 9:43 am
by Marc56us
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
Quickly created program, you can reduce.
(I hope the team will put back one of the tested timoute functions in betas.)
it's just a simple GET request to grab the HTML from a websit
Why not use ReceiveHTTPFile() ? A single line would be enough.
PS. In my tests, HTTPRequest and ReceiveHTTPFile abort after 21 sec.
:wink:

Re: HTTPRequest Timeout - How to abort the request?

Posted: Sun Aug 21, 2022 4:31 am
by phaselock.studio
Marc56us wrote: Sat Aug 20, 2022 9:43 am
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
Quickly created program, you can reduce.
(I hope the team will put back one of the tested timoute functions in betas.)
it's just a simple GET request to grab the HTML from a websit
Why not use ReceiveHTTPFile() ? A single line would be enough.
PS. In my tests, HTTPRequest and ReceiveHTTPFile abort after 21 sec.
:wink:
This is exactly what I needed, thank you!!

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?

Posted: Sun Aug 21, 2022 9:08 am
by Marc56us
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?
No need to know in advance the name of the root filename (homepage)
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
:wink:

Re: HTTPRequest Timeout - How to abort the request?

Posted: Sun Aug 21, 2022 10:01 am
by infratec
Simply use libcurl directly and set a timeout of 1 or 2 seconds. :wink:

Re: HTTPRequest Timeout - How to abort the request?

Posted: Mon Aug 22, 2022 2:56 am
by phaselock.studio
Marc56us wrote: Sun Aug 21, 2022 9:08 am
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?
No need to know in advance the name of the root filename (homepage)
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
:wink:
Very cool to know. Thanks Marc56us!

Re: HTTPRequest Timeout - How to abort the request?

Posted: Mon Aug 22, 2022 3:00 am
by phaselock.studio
infratec wrote: Sun Aug 21, 2022 10:01 am Simply use libcurl directly and set a timeout of 1 or 2 seconds. :wink:
Please excuse my noobness but the only way to do this would be through a call to RunProgram() right?

Re: HTTPRequest Timeout - How to abort the request?

Posted: Mon Aug 22, 2022 7:22 am
by infratec
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.

Re: HTTPRequest Timeout - How to abort the request?

Posted: Mon Aug 22, 2022 7:57 am
by Marc56us
Timeout was removed from PB 6 LTS

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
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

:wink:

Re: HTTPRequest Timeout - How to abort the request?

Posted: Mon Aug 22, 2022 9:27 am
by infratec
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.