Main Menu

HTTP/FTP Lib

Started by empty, June 01, 2005, 10:19:12 AM

Previous topic - Next topic

empty

See post below

empty

#1
I've cleaned up the thread.
The library is now downloadable (see next post)

Here's an example code for HttpLib:

PlayBASIC Code: [Select]
#INCLUDE "HttpLib"


; Get Free Http Index
Http = GetFreeHttp_()

; Specify the Agent name (default: Mozilla 4.0 (compatible; PlayBasi)
HttpAgent("PlayBasic Testing...")

; Open a new session
HttpOpen(Http)

; Connect to a URL
HttpConnect(Http, "www.underwaredesign.com")

; Start a "GET" request (for a "POST" request use the second Parameter to send data)
HttpRequestData(Http, "")
loading$ = "Loading "

; HttpPollTransfer transfers by default a maximum of 1024 bytes
; and returns the number of actually transfered bytes
trans = HttpPollTransfer(Http)

While trans <> 0
Cls 0
Print loading$
loading$ = loading$ + "."
trans = HttpPollTransfer(Http)
Sync
EndWhile

; There is no way to check the length (file size) of text based files
; so we assume that when 0 bytes were transmitted the transfer is completed.
; After HttpPollTransfer returns 0 the transfered data can be read
; with either HttpDataAsString or HttpCopyDataToBank
Content$ = HttpDataAsString(Http)

; When the data has been read close the transfer to release the memory
HttpCloseTransfer(Http)

; Display the stuff
Dim Lines$(0)
SplitLines(Content$, Lines$())

Cls 0
For i = 0 To GetArrayElements(Lines$(), 1)-1
Print Lines$(i)
Next i
Sync
WaitKey:WaitNoKey


; Close the connection
HttpDisconnect(Http)

; Open a new connection
HttpConnect(Http, "www.underwaredesign.com/screens/PlayBasic/MissileAttack_Med.jpg")

; Request GET data
HttpRequestData(Http, "")

; Initiate a file download
HttpDownloadFile(Http, "c:\_1_screenshot.jpg")

; We can check the length (file size) of binary files with
; HttpGetLength and the number of bytes read with HttpGetBytesRead
loading$ = "Downloading "

While HttpGetBytesRead(Http) < HttpGetLength(Http)
Cls 0
Print loading$
percent = HttpGetBytesRead(Http) * 100 / HttpGetLength(Http)
Print "Percent: " + Str$(percent) + "%"
loading$ = loading$ + "."
trans = HttpPollTransfer(Http)

Sync
EndWhile
Cls 0
Print "Finished"
Print "Percent: 100%"
; Close the transfer
HttpCloseTransfer(Http)

; load and draw the downloaded image
LoadImage "c:\_1_screenshot.jpg", 1
DrawImage 1, 100, 100, 0
Sync

; Close Http
HttpClose(Http)

WaitKey:WaitNoKey




Psub SplitLines(s$, Lines$())
ReDim Lines$(0)
c = 0
For i = 1 To Len(s$)
If Mid(s$, i) <> 10 And Mid(s$, i) <> 13
Lines$(c) = Lines$(c) + Mid$(s$, i, 1)
Else
Inc c
ReDim lines$(c)
EndIf
Next i
EndPsub



empty

#2
I've uploaded the HttpLib. Unzip it and (for now) place the HttpLib.pba file in the "Library" folder. The *.pli files are of no use with the current IDE version.



empty

#3
New version:

Obsolete files removed:  Http is standard library. See PlayBASIC help files


Note:
A few function names changed. See demo source code above.