TCP_Example1: download a page

<< Click to Display Table of Contents >>

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

TCP_Example1: download a 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

USES "TCPUDP"

 

'---Load File module

USES "FILE"

 

'---Define some global variables and init them

Dim MyPage As String = "http://www.thinbasic.com/index.php"

Dim MySite As String = "www.thinbasic.com"

 

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: http://www.thinbasic.com/")

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

TCP_Print(nFile, "")

 

'---Get back data in a loop

Do                                            

INCR Count

 sBuffer = TCP_Recv(nFile, 4096)

 sPage = sPage & sBuffer

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

 

'---Close the channel

TCP_Close(nFile)

 

'---Save the page

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