Page 1 of 1

[SOLVED] Cannot get simple network request/response working

Posted: Wed Aug 31, 2016 4:55 pm
by SparrowhawkMMU
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

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
Could somebody point out my errors? (many, I'm sure).

Thanks :)

Re: Cannot get simple network request/response working

Posted: Wed Aug 31, 2016 4:56 pm
by SparrowhawkMMU
Sorry, forgot the tech details:

OS X 10.9.5, PB 5.50 (full)

Re: Cannot get simple network request/response working

Posted: Wed Aug 31, 2016 8:18 pm
by normeus
use PB 5.42LTS and on compiler options turn off unicode.

The Chinese response gave it away. I wonder if people in China see unlegible characters as English?

You don't have PB 5.42 you say?

Well then use one of the easy tutorials to go from unicode to "ASCII" on 5.50.

( I don't know where these tutorials are so just download 5.42 from your PB account )

EDIT:
(sorry about the snarky comments ) :oops:
replace these lines of code:

Code: Select all

                response$ + PeekS(@buffer$,1024, #PB_Ascii)
               
               PrintN(#LF$ + "Latest data: " + PeekS(@buffer$,1024, #PB_Ascii))

Norm.

Re: Cannot get simple network request/response working

Posted: Wed Aug 31, 2016 8:37 pm
by JHPJHP
Hi SparrowhawkMMU,

See if the following has anything you can use: Services, Stuff, and Shellhook.
- Stuff\ProtocolStuff\HTTP.pb

The script opens a window with two fields, Username and Password. Submitting the proper credentials will download a copy of PureBasic using the Functions SendNetworkString and ReceiveNetworkData.

Re: Cannot get simple network request/response working

Posted: Thu Sep 01, 2016 7:02 am
by infratec
Hi,

try this:

Code: Select all

EnableExplicit

Define id.i, request$, Current$ , response$, Length.i, *Buffer, Timeout.i


If InitNetwork()
  
  If OpenConsole()
    
    id = OpenNetworkConnection("purebasic.com", 80)
    If id
      
      request$ = "GET / HTTP/1.1" + #LF$
      request$ + "Host: purebasic.com" + #LF$
      request$ + #LF$
      
      PrintN("Request: " + request$)
      
      SendNetworkString(id, request$, #PB_UTF8)
      
      *Buffer = AllocateMemory(1024)
      If *Buffer
        
        Timeout = 1000
        Repeat
          
          Select NetworkClientEvent(id)
            Case #PB_NetworkEvent_None
              Delay(10)
              Timeout - 1
            
            Case #PB_NetworkEvent_Data
              Length = ReceiveNetworkData(id, *Buffer, MemorySize(*Buffer))
              If Length
                Current$ = PeekS(*Buffer, Length, #PB_UTF8)
                response$ + Current$
                PrintN(#LF$ + "Latest data: " + Current$)
                If FindString(Current$, "</html>")
                  Break
                EndIf
              Else
                Break
              EndIf
              
            Case #PB_NetworkEvent_Disconnect         
              PrintN(#LF$ + "Disconnected!")
              Break
              
          EndSelect
          
        Until Timeout = 0
        
        FreeMemory(*Buffer)
      EndIf
      
      PrintN(#LF$ + "Response: "  + response$)
      
      PrintN(#LF$ + "Press RETURN to end process")
      Input()
      
    EndIf
    
  Else
    Debug "Failed to open console"
    
  EndIf
  
  CloseNetworkConnection(id)
  
Else
  Debug "Could not establish network connection"
  
EndIf
Bernd

Re: Cannot get simple network request/response working

Posted: Thu Sep 01, 2016 9:30 am
by SparrowhawkMMU
Thanks everyone for your help - I'll take a look through all your examples today.

Hopefully with this, I finally have all the bits of PB knowledge I need to get my first non-trivial PB app started.

Again, thanks.

PS @normeus: "The Chinese response gave it away. I wonder if people in China see unlegible characters as English?" That would indeed be amusing ;)