• Welcome to Jose's Read Only Forum 2023.
 

SDL: Getting the contents of a web page using SDL_NET

Started by José Roca, July 27, 2008, 11:28:59 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

 
Simple http get example using the SDL_net library.

DLL dependencies: SDL.DLL, SDL_NET.DLL.


' Simple http get example using the SDL_net library

#INCLUDE "SDL_NET.INC"

%RECVBUFFLEN = 8192
$DEFAULT_HOST = "www.powerbasic.com"

FUNCTION PBMAIN () AS LONG

   LOCAL szHostName AS ASCIIZ * 260
   LOCAL strBuffer AS STRING
   LOCAL szRecvBuffer AS ASCIIZ * %RECVBUFFLEN + 1
   LOCAL nBytes AS LONG
   LOCAL ip AS IPAddress
   LOCAL psocket AS DWORD

   szHostName = $DEFAULT_HOST

   IF SDLNet_Init <> 0 THEN
      MSGBOX "SDLNet_Init failed"
      EXIT FUNCTION
   END IF

   IF SDLNet_ResolveHost(VARPTR(ip), szHostName, 80) <> 0 THEN
      MSGBOX "SDLNet_ResolveHost failed"
      GOTO Terminate
   END IF

   psocket = SDLNet_TCP_Open(VARPTR(ip))
   IF psocket = %NULL THEN
      MSGBOX "SDLNet_TCP_Open failed"
      GOTO Terminate
   END IF

   strBuffer = "GET / HTTP/1.0" + $CRLF & _
               "Host: " & szHostName & $CRLF & _
               "Connection: close" & $CRLF & _
               "User-Agent: GetHTTP 0.0" + $CRLF & $CRLF

   IF SDLNet_TCP_Send(psocket, STRPTR(strBuffer), LEN(strBuffer)) < LEN(strBuffer) THEN
      MSGBOX "SDLNet_TCP_Send failed""
      GOTO Terminate
   END IF

   strBuffer = ""
   DO
      nBytes = SDLNet_TCP_Recv(psocket, VARPTR(szRecvBuffer), %RECVBUFFLEN)
      IF nBytes <= 0 THEN EXIT DO
      strBuffer = strBuffer & LEFT$(szRecvBuffer, nBytes)
   LOOP
   MSGBOX strBuffer

Terminate:

   IF pSocket THEN SDLNet_TCP_Close(psocket)
   SDLNet_Quit

END FUNCTION