[SOLVED] Cannot get simple network request/response working

Just starting out? Need help? Post your questions and find answers here.
User avatar
SparrowhawkMMU
User
User
Posts: 56
Joined: Fri Jan 17, 2014 8:55 pm
Location: UK

[SOLVED] Cannot get simple network request/response working

Post 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 :)
Last edited by SparrowhawkMMU on Thu Sep 01, 2016 9:45 am, edited 1 time in total.
User avatar
SparrowhawkMMU
User
User
Posts: 56
Joined: Fri Jan 17, 2014 8:55 pm
Location: UK

Re: Cannot get simple network request/response working

Post by SparrowhawkMMU »

Sorry, forgot the tech details:

OS X 10.9.5, PB 5.50 (full)
normeus
Enthusiast
Enthusiast
Posts: 484
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Cannot get simple network request/response working

Post 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.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
User avatar
JHPJHP
Addict
Addict
Posts: 2279
Joined: Sat Oct 09, 2010 3:47 am

Re: Cannot get simple network request/response working

Post 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.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
infratec
Always Here
Always Here
Posts: 7699
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Cannot get simple network request/response working

Post 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
User avatar
SparrowhawkMMU
User
User
Posts: 56
Joined: Fri Jan 17, 2014 8:55 pm
Location: UK

Re: Cannot get simple network request/response working

Post 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 ;)
Post Reply