Date: 02-16-2022
Return to Index
created by gbSnippets
'Downloading a large or small file is essentially the same code - except that with a
'large file multiple TCP Recv commands are needed. This can be handled in a simple
'Do loop, ending the loop when no more data is received (or when an error is received).
'Primary Code:
'This example shows only the basics - open a connection, send the request,
'receive the data (looping until all data or error received) and close the connection.
Tcp Open "HTTP" AT "www.garybeene.com" AS #1 TIMEOUT 60000
Tcp Print #1, "GET /files/tcp_test.txt HTTP/1.0"
Tcp Print #1, ""
Do
Tcp Recv #1, 4096, Buffer$
AllData$ = AllData$ + Buffer$
Loop While IsTrue Len(Buffer$) AND IsFalse Err
Tcp Close #1
'Compilable Example: (Jose Includes)
'This code fleshes out the basic example above, but even then you can see
'that the code is still pretty short.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg as Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 50,10,100,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
MsgBox GetFileContent ("www.garybeene.com", "http://www.garbeene.com/gbsnippet.msg")
End If
End Function
Function GetFileContent(Site$, File$) As String
Local Buffer$, LargeFileName$
Local Entire_page$, Htmlfile$, Link$
Local Length&
LargeFileName$ = "gbsnippets.msg"
Site$ = "www.garybeene.com"
File$ = "http://www.garybeene.com/files/" + LargeFileName$ + "gbsnippets.msg"
'Connect
Tcp Open "http" AT Site$ AS #1 TIMEOUT 60000
If Err Then Beep : Exit Function
' Send the GET request...
Tcp Print #1, "GET " & File$ & " HTTP/1.0"
Tcp Print #1, "Referer: http://www.garybeene.com"
Tcp Print #1, "User-Agent: gbSnippets"
Tcp Print #1, ""
' Retrieve the page...
Do
Tcp Recv #1, 4096, Buffer$
Entire_page = Entire_page + Buffer$
Loop While IsTrue Len(Buffer$) AND IsFalse Err
' Close the TCP/IP port...
Tcp Close #1
' Save the file
Open Exe.path$ + LargeFileName$ For Output as #1
Print #1, Buffer$
Close #1
Function = Entire_Page
End Function
'gbs_00371
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm