Created
June 25, 2020 00:38
-
-
Save EthanThatOneKid/bc25cfc1e5b36c2fcd09332590e5b8b6 to your computer and use it in GitHub Desktop.
HTTP GET request in QB64.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| IF Download("www.qb64.org/favicon.ico", "favicon.ico", 10) THEN ' timelimit = 10 seconds | |
| SCREEN _LOADIMAGE("favicon.ico", 32) | |
| ELSE: PRINT "Couldn't download image." | |
| END IF | |
| SLEEP | |
| SYSTEM | |
| ' ---------- program end ----------- | |
| FUNCTION Download (url$, file$, timelimit) ' returns -1 if successful, 0 if not | |
| url2$ = url$ | |
| x = INSTR(url2$, "/") | |
| IF x THEN url2$ = LEFT$(url$, x - 1) | |
| client = _OPENCLIENT("TCP/IP:80:" + url2$) | |
| IF client = 0 THEN EXIT FUNCTION | |
| e$ = CHR$(13) + CHR$(10) ' end of line characters | |
| url3$ = RIGHT$(url$, LEN(url$) - x + 1) | |
| x$ = "GET " + url3$ + " HTTP/1.1" + e$ | |
| x$ = x$ + "Host: " + url2$ + e$ + e$ | |
| PUT #client, , x$ | |
| t! = TIMER ' start time | |
| DO | |
| _DELAY 0.05 ' 50ms delay (20 checks per second) | |
| GET #client, , a2$ | |
| a$ = a$ + a2$ | |
| i = INSTR(a$, "Content-Length:") | |
| IF i THEN | |
| i2 = INSTR(i, a$, e$) | |
| IF i2 THEN | |
| l = VAL(MID$(a$, i + 15, i2 - i -14)) | |
| i3 = INSTR(i2, a$, e$ + e$) | |
| IF i3 THEN | |
| i3 = i3 + 4 'move i3 to start of data | |
| IF (LEN(a$) - i3 + 1) = l THEN | |
| CLOSE client ' CLOSE CLIENT | |
| d$ = MID$(a$, i3, l) | |
| fh = FREEFILE | |
| OPEN file$ FOR OUTPUT AS #fh: CLOSE #fh ' erase existing file? | |
| OPEN file$ FOR BINARY AS #fh | |
| PUT #fh, , d$ | |
| CLOSE #fh | |
| Download = -1 'indicates download was successfull | |
| EXIT FUNCTION | |
| END IF ' availabledata = l | |
| END IF ' i3 | |
| END IF ' i2 | |
| END IF ' i | |
| LOOP UNTIL TIMER > t! + timelimit ' (in seconds) | |
| CLOSE client | |
| END FUNCTION |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment