[SOLVED] Cannot get simple network request/response working
Posted: Wed Aug 31, 2016 4:55 pm
Hello,
I cannot get this seemingly simple code working. Three things happen:
1) the response only ever triggers #PB_NetworkEvent_Data once
2) the response consists of chinese characters
3) the response length is never zero so the script only ends when the #PB_NetworkEvent_Disconnect event is trapped
Could somebody point out my errors? (many, I'm sure).
Thanks
I cannot get this seemingly simple code working. Three things happen:
1) the response only ever triggers #PB_NetworkEvent_Data once
2) the response consists of chinese characters
3) the response length is never zero so the script only ends when the #PB_NetworkEvent_Disconnect event is trapped
Code: Select all
EnableExplicit
Define id.i
Define request$
Define buffer$
Define response$
If InitNetwork()
If OpenConsole()
id = OpenNetworkConnection("purebasic.com", 80)
request$ = "GET / HTTP/1.1" + Chr(10) + "Host: purebasic.com"
PrintN(#LF$ + "Request: " + request$)
SendNetworkString(id, request$ + Chr(10) + Chr(10))
response$ = ""
Repeat
Select NetworkClientEvent(id)
Case #PB_NetworkEvent_Data
buffer$ = Space(1024)
ReceiveNetworkData(id, @buffer$, 1024)
response$ + buffer$
PrintN(#LF$ + "Latest data: " + buffer$)
If Len(Trim(buffer$)) = 0
Break
EndIf
Case #PB_NetworkEvent_Disconnect
PrintN(#LF$ + "Disconnected!")
Break
EndSelect
ForEver
PrintN(#LF$ + "Response: " + response$)
PrintN(#LF$ + "Press a key to end process")
Input()
Else
Debug "Failed to open console"
EndIf
CloseNetworkConnection(id)
Else
Debug "Could not establish network connection"
EndIf
End
Thanks