Readline and streams

BlitzMax Forums/BlitzMax Programming/Readline and streams

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

SocketReadAvail helped alot. Here is my newest code,but only works with sites that use content-length (Appears most do not)

Global WebSock:tSocket ' our lovely socket
Global WebStream:tSocketStream ' our lovely stream
Global TheURL:String=""
Global SessionCookie:String="" ' cookie for saving web session

Local TXT:String

HTTPConnect("www.asitethatsupportscontentlength.com")
TXT=HTTPRequest("")
Print "Body--------"
Print TXT

Print "Closing"

HTTPClose()
Print "Done"

	
' connect to server
	Function HTTPConnect(sURL:String,iTimeOut:Int=5000)
		TheURL=sURL
		WebSock=CreateTCPSocket()
		ConnectSocket(WebSock,HostIp(sURL),80)
		iTimeOut = iTimeOut + MilliSecs()
		While Not SocketConnected(WebSock) 
			If MilliSecs()>iTimeOut Then
				Return 0 ' failed due to timeout
			End If
		Wend
     ' We are Connected, create our streamer
		WebStream=CreateSocketStream(WebSock,False)
		Return 1 ' success
	End Function
	
' Requests data from web server
	Function HTTPRequest:String(sPage:String,iTimeOut:Int=5000)
		Local TXT:String,A:Int
		Local tBody:String="x" ' body of html
		Local tHeader:String="x" ' header of html
		Local NL:String= Chr(13) + Chr(10)
		Local ContentLength:Int
		' Form a HTTP 1.1 Get Request
		TXT="GET /" + sPage + " HTTP/1.1" + NL
		TXT=TXT + "Host: " + TheURL + NL
		TXT=TXT + "User-Agent: BlitzBrowser" + NL
		TXT=TXT + "Accept: */*" + NL
		'TXT=TXT + "Content-Length: 0" + NL
		If SessionCookie<>"" Then
			TXT = TXT + SessionCookie 
		End If
		WriteLine WebStream, TXT
		FlushStream(WebStream)
		' now read in header grabbing length
		iTimeout=iTimeOut + MilliSecs()
		TXT=""
		Repeat 
			If MilliSecs()>iTimeOut And SocketReadAvail(WebSock)=0 Then Exit
			If SocketReadAvail(WebSock)>0 Then
				If tHeader="x" Then
					' header not complete yet
					TXT=TXT + ReadString(WebStream,SocketReadAvail(WebSock))
					' if end of header has been reached
					A=Instr(TXT,NL+NL,1)
					If A>1 Then 
						 ' grab header, remainder is body
						tHeader=Mid(TXT,1,A+2)
						tBody=Mid(TXT,Len(tHeader)+1)
						' save content length
						A=Instr(tHeader,"Content-Length:",1)
						TXT=Mid(tHeader,A+Len("Content-Length:"))
						TXT=Mid(TXT,1,Instr(TXT,NL,1))
						ContentLength=Int(TXT)
						TXT=""
						' save ASP session cookie
						If Instr(tHeader,"Set-Cookie: ASP",1)
							A=Instr(tHeader,"Set-Cookie: ASP",1)
							TXT=Mid(tHeader,A+Len("Set-Cookie:"))
							TXT=Mid(TXT,1,Instr(TXT,NL,1)-1)
							SessionCookie="Cookie:" + TXT + NL
							TXT=""
						End If
						' in case we get the whole thing in one shot check body length
						If Len(tbody)>=ContentLength Then Exit
					End If
				Else
					tBody=tBody + ReadString(WebStream,SocketReadAvail(WebSock))
					If Len(tbody)>=ContentLength Then Exit
				End If				
			End If
		Forever
		Print "Header-----"
		Print tHeader
		
		'Print "Body-------"
		'Print tBody
		Return tBody
		
	End Function
	
	' Close Connection
	Function HTTPClose()
		SessionCookie=""
		CloseStream(WebStream)
		CloseSocket(WebSock)
	End Function