Date: 02-16-2022
Return to Index
created by gbSnippets
'The header and body of an HTTP response is separated by two $crlf
'character pairs, making it easy to separate the two. The headers,
'consist of multiple lines of NAME : VALUE pairs and can be separated
'Here's an example header returned by a server, showing an initial line
'following by multiple lines of Name:Value pairs.
HTTP/1.1 200 OK
Date: Thu, 01 Oct 2009 03:02:37 GMT
Server: Apache
Last-Modified: Thu, 01 Oct 2009 02:19:54 GMT
Content-Length: 36
'Primary Code:
'This code puts each line of the header into an array. Position 0 will
'have the initial line. All other positions will have a string with the
'format of NAME : VALUE.
sHeader = Extract$(sDataFromHTTPServer, $CRLF + $CRLF)
sBody = Remain$(sDataFromHTTPServer, $CRLF + $CRLF)
Local Dim D(ParseCount(sHeader, $crlf))
Parse(D, $crlf)
'Then for each array element you can separate the Name from Value with:
sName$ = Extract$(D(n), ":")
sValue$ = Remain$(D(n), ":")
'Compilable Example: (Jose Includes)
'I have a small, two-line file called "tcp_test.txt" on my server that is
'used in this example. The code sends an HTTP request. The response
'will contain both a header and the body of the requested file.
#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
Local Buffer$, sHeader$, sBody$
Tcp Open "HTTP" At "www.garybeene.com" As #1 TimeOut 60000
Tcp Print #1, "GET http://www.garybeene.com/files/tcp_test.txt HTTP/1.0"
Tcp Print #1, ""
Tcp Recv #1, 4096, Buffer$
Tcp Close #1
'Buffer$ now the header and the body.
sHeader$ = Extract$(Buffer$, $CrLf + $CrLf)
'display body
sBody$ = Remain$(Buffer$, $CrLf + $CrLf)
'create array each line of header
Dim D(ParseCount(sHeader$, $CrLf)) As String
Parse sHeader$, D(), $CrLf
'for example, let's display line 2, then it's parts
MsgBox "Full Line = " + D(2)
MsgBox Extract$(D(2), ":")
MsgBox Remain$(D(2), ":")
End If
End Function
'gbs_00377
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm