Navigation:  thinBasic Modules > TcpUdp (Networking) > TCP functions > TCP_Examples >

TCP_Example1: download a page

Previous pageReturn to chapter overviewNext page


' - connect to a web site

' - ask for a page

' - save it into a buffer

' - save the buffer into a text file



'---Load TCP module



'---Load File module


'---Define some global variables and init them

Dim MyPage As String = ""

Dim MySite As String = ""


Dim sBuffer As String

Dim sPage   As String    

Dim Count   As Long

'---Get a file number

Dim nFile   As Long = Tcp_FreeFile

'---Open a connection

TCP_Open("http", MySite, nFile)


'---Send request

TCP_Print(nFile, "GET " & MyPage & " HTTP/1.0")

TCP_Print(nFile, "Referer:")

TCP_Print(nFile, "User-Agent: TCP Test using thinBasic")

TCP_Print(nFile, "")


'---Get back data in a loop


INCR Count

 sBuffer = TCP_Recv(nFile, 4096)

 sPage = sPage & sBuffer

Loop While ((Len(sBuffer) > 0) And (ERR = 0))


'---Close the channel



'---Save the page

File_Save(APP_SourcePath & "Page.txt", sPage)