' [S]imple [N]etwork [C]onnection
#include once "snc.bi"

#include once "snc_utility.bi"

' test of a client connection 

dim as string ServerName = "www.worldcommunitygrid.org"
dim as string ServerFile = "/team/viewTeamMemberDetail.do?sort=name&teamId=G5XMSGSVR1&pageNum=1&numRecordsPerPage=500&xml=true"
dim as string FileType   = MIME_HTM & ";" & MIME_TXT

' connect to web server at port 80
dim as NetworkClient client=type(ServerName,80)
' get a connection from ConnectionFactory
var connection = client.GetConnection()
' build an HTTP GET request
dim as string request =HTTPGet(ServerName,ServerFile,FileType)

' ready to send ?
while connection->CanPut()<>1
  sleep 100
wend
' put data on the connection
connection->PutData(strptr(request),len(request))
' ready to receive ?
while connection->CanGet()<>1
  sleep 100
wend

dim as zstring ptr buffer
var nBytes = connection->GetData(buffer)
print "number of received bytes " & nBytes


' get last char position of the HTTP asci header
var LastChar = instr(*buffer,HeaderEnd)-1
var Header   =  left(*buffer,LastChar)
' no OK answer ?
if instr(Header,"200 OK")<1 then
  print header
  print "can't get " & ServerName & ServerFile & " !"
  beep:sleep:end
end if
' get first byte behind the HTTP header
var DataStart=LastChar+4
' save it
open "document.xml" for binary access write as #1
dim as ubyte ptr FileBuffer=@buffer[DataStart]
nBytes-=DataStart
put #1,,*FileBuffer,nBytes
close #1
print "file saved ..."
' free the buffer (allocate by snc.bi)
deallocate buffer
sleep

