• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

Just test if a download would work?

Started by Theo Gottwald, September 16, 2013, 08:43:33 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Theo Gottwald

We have some code sort of "WEBGET" to download a file.
What if i do not want to download the file, but just check if the download link is valid?
Has anybody some code/idea for that?

Chris Holbrook

Does the HEAD method help? If you look at the XHR documentation, you have GET, POST, HEAD and some small change.

Theo Gottwald

#2
Ok, that was a good idea, now i found some links as a starting point.

Download with Progress

Download to String

and for me less interesting:

FTP GetFile

httpGet  DL-SpeedTest

Maybe something like this?
If U03=0 it should download all of the file otherwise just the first 4096 Bytes.

'######################################################################################################################
'######################################################################################################################
'  site = "www.nbson.com"
'  file = "/speedData/httpTest.html"
FUNCTION httpGet(BYVAL U01 AS STRING,OPT BYVAL U03 AS BYTE) AS LONG
  LOCAL buffer AS STRING
  LOCAL temp   AS STRING
  LOCAL fNumber AS LONG
  LOCAL T01,T02,T03 AS LONG
  LOCAL file,site AS STRING

  U01=LCASE$(TRIM$(U01))
  T02=INSTR(U01,"//")
  IF zero(T02) THEN T03=1 ELSE T03=T02+2
  T02=INSTR(T03,U01,"/")
  IF zero(T02) THEN GOTO enx
  Site=MID$(U01,1 TO T02-1)
  file=MID$(U01,T02)

   fNumber=FREEFILE

   TCP OPEN PORT 80 AT site AS #fNumber TIMEOUT 10000
   IF ERR THEN
        FUNCTION= -1
        EXIT FUNCTION
   END IF

   TCP PRINT #fNumber, "GET " & file & " HTTP/1.1"
   TCP PRINT #fNumber, "Accept: */*"
   TCP PRINT #fNumber, "Accept-Language: en-us"
   TCP PRINT #fNumber, "Host: " & site
   TCP PRINT #fNumber, "Pragma: no-cache"
   TCP PRINT #fNumber, "Referer: http://"+site+"/"
   TCP PRINT #fNumber, "UserAgent: webget 1.2 ("+ site +")"
   TCP PRINT #fNumber, ""
   DO
     TCP RECV #fNumber, 4096, buffer
     temp = temp & buffer
     IF U03=1 THEN T01=1: EXIT LOOP
     IF ERR THEN T01=0:EXIT LOOP
   LOOP WHILE LEN(buffer)

   ' ** close the tcp connection
   TCP CLOSE #fNumber
   enx:
   FUNCTION=T01
   EXIT FUNCTION
END FUNCTION