This seems to work for retrieving the raw content of a viewable webpage (ie. whatever is sent to the client).
The example page is in plain text but it should work with html, css and javascript too (as well as binary files).
As for parsing the HTML, that's up to you
rem This library contains functionality for interfacing with the Internet from Windows
#constant wininet 1
load dll "wininet.dll", wininet
rem Open an internet handle
hInternet = InternetOpen("Internet Access From DBPro", 1, "", "", 0)
rem Open target URL
hFile = InternetOpenUrl(hInternet, "http://db.gamefaqs.com/console/xbox360/file/grand_theft_auto_v_ach.txt", "", 0, 0x80000000, 0)
rem Create a buffer of 100 kilobytes and read as much of the target URL that fits into that
pBuffer = alloc zeroed(100 * 1024 * 1024)
strlen = InternetReadFile(hFile, pBuffer, 100 * 1024 * 1024)
rem Get html / whatever format the loaded page is in as a string (the data is in pBuffer now);
rem this will obviously not work with binary file types that contain zero (null) bytes
html$ = peek string(pBuffer, strlen)
rem Free buffer
free pBuffer
rem Release internet handles
InternetCloseHandle(hFile)
InternetCloseHandle(hInternet)
rem The source will likely be quite long; we'll just copy it to the clipboard here.
rem Paste it into a text editor to see whether it works.
write to clipboard html$
exit prompt "Retrieved " + str$(strlen / 1024.0, 2) + " Kb from url", "Data copied to clipboard!"
rem That's it!
end
rem Wrapper functions for functions in Wininet.dll
function InternetOpen(strAgent as string, dwAccessType as dword, strProxyName as string, strProxyBypass as string, dwFlags as dword)
rem Handle empty strings as null pointers
pstrProxyName = MakeSTring(strProxyName)
pstrProxyBypass = MakeString(strProxyBypass)
handle = call dll(wininet, "InternetOpenA", strAgent, dwAccessType, pstrProxyName, pstrProxyBypass, dwFlags)
FreeString(pstrProxyName)
FreeString(pstrProxyBypass)
endfunction handle
function InternetOpenUrl(hInternet as dword, strUrl as string, strHeaders as string, dwHeadersLength as dword, dwFlags as dword, dwContext as dword)
pstrHeaders = MakeString(strHeaders)
handle = call dll(wininet, "InternetOpenUrlA", hInternet, strUrl, pstrHeaders, dwHeadersLength, dwFlags, dwContext)
FreeString(pstrHeaders)
endfunction handle
function InternetReadFile(hFile as dword, pBuffer as dword, dwBytesToRead as dword)
pBytesRead = make memory(4)
success = call dll(wininet, "InternetReadFile", hFile, pBuffer, dwBytesToRead, pBytesRead)
if success then readBytes = *pBytesRead
delete memory pBytesRead
endfunction readBytes
function InternetCloseHandle(hInternet as dword)
success = call dll(wininet, "InternetCloseHandle", hInternet)
endfunction success
rem Copies the provided string into a memory buffer whose pointer is returned. Will return a null pointer
rem if an empty string is provided.
function MakeString(s as string)
if fast len(s) < 1 then exitfunction 0
ptr = alloc zeroed(fast len(s) + 1)
poke string ptr, s
endfunction ptr
rem Frees a string pointer created using the above function.
rem Will do nothing if a null pointer is provided.
function FreeString(pStr as dword)
if pStr then free pStr
endfunction
Requires the Matrix1Utilities plugins by the way, but that is mostly for convenience; you could rewrite it using memblocks instead (which would however likely be slower).
"Why do programmers get Halloween and Christmas mixed up?" Because Oct(31) = Dec(25)