The following code connects with a web page and retrieves its html text.
' ########################################################################################
' The following example illustrates the use of the InternetOpen function.
' ########################################################################################
#COMPILE EXE
#DIM ALL
#INCLUDE "WININET.INC"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN () AS LONG
LOCAL bResult AS LONG
LOCAL hSession AS DWORD
LOCAL hInternet AS DWORD
LOCAL szUrl AS ASCIIZ * %INTERNET_MAX_PATH_LENGTH
LOCAL strBuffer AS STRING
LOCAL strData AS STRING
LOCAL cbBytesRead AS DWORD
' Initializes an application's use of the WinINet functions.
hSession = InternetOpen("Test", %INTERNET_OPEN_TYPE_DIRECT, "", "", 0)
IF hSession = %NULL THEN
? "InternetOpen error:" & STR$(GetLastError)
#IF %DEF(%PB_CC32)
WAITKEY$
#ENDIF
EXIT FUNCTION
END IF
' Opens a resource specified by a complete HTTP URL.
szUrl = "http://www.powerbasic.com/"
hInternet = InternetOpenUrl(hSession, szUrl, "", 0, %INTERNET_FLAG_NO_CACHE_WRITE, 0)
IF hInternet = %NULL THEN
? "InternetOpenUrl error:" & STR$(GetLastError)
InternetCloseHandle hSession
#IF %DEF(%PB_CC32)
WAITKEY$
#ENDIF
EXIT FUNCTION
END IF
' Reads the data in 1024 bytes chunks
strBuffer = SPACE$(1024)
DO
bResult = InternetReadFile(hInternet, STRPTR(strBuffer), LEN(strBuffer), cbBytesRead)
IF bResult = 0 OR cbBytesRead = 0 THEN EXIT DO
IF cbBytesRead = LEN(strBuffer) THEN
strData = strData & strBuffer
ELSE
strData = strData & LEFT$(strBuffer, cbBytesRead)
END IF
LOOP
' Closes handles
InternetCloseHandle hInternet
InternetCloseHandle hSession
' Displays the retrieved data
? strData
#IF %DEF(%PB_CC32)
WAITKEY$
#ENDIF
END FUNCTION
' ========================================================================================