by Brent Thorn - BrentDT Originally in Liberty BASIC Newsletter #37
This demo reads a remote file into memory. Hopefully Yahoo doesn't mind my choice of URLs. Please note the strange hanging behavior at the "Close #inet" statement.
After doing a little testing, I found my hanging was the result of some ad-blocking software I have installed. When I disabled it the hanging ceased. It may be rare but you may want to keep this possibility in mind when using my code in your own programs. I can only suggest not closing #inet until after all your windows are closed at exit to avoid any possibility of a user's thinking your app is faulty.
Demo
'CODE BEGINS'Retrieving HTTPS Data Demo'By Brent D. Thorn, 9/2005
remoteFile$ ="https://login.yahoo.com/"
CHUNK.SIZE =512' enough bytes to hold a single line of HTMLOpen"wininet"ForDLLAs#inet
'It would be a good idea to test for an Internet'connection before proceding. This demo assumes'you have one already.' Register a new user agent. Use proxy settings' set up in Internet Options Control Panel.CallDLL#inet,"InternetOpenA", _
"My User Agent"AsPtr, _
0AsLong, _ 'INTERNET_OPEN_TYPE_PRECONFIG
_NULL AsLong, _
_NULL AsLong, _
0AsULong, _
hInet AsULongIf hInet =0Then[ErrExit]' Open a request to a remote fileCallDLL#inet,"InternetOpenUrlA", _
hInet AsULong, _
remoteFile$ AsPtr, _
_NULL AsLong, _
0AsLong, _
2147483648AsULong, _ 'INTERNET_FLAG_RELOAD
_NULL AsLong, _
hFile AsULongIf hFile =0Then[ErrExit]' Start reading in the file by chunks.
Buffer$ =""
chunk$ =Space$(CHUNK.SIZE)+Chr$(0)Struct pdw, NumberOfBytesRead AsULongDoCallDLL#inet,"InternetReadFile", _
hFile AsULong, _
chunk$ AsPtr, _
CHUNK.SIZE AsULong, _
pdw AsStruct, _
ret AsLongIf pdw.NumberOfBytesRead.struct=0ThenExitDo
Buffer$ = Buffer$ +Left$(chunk$, pdw.NumberOfBytesRead.struct)LoopUntil0Print Buffer$
[ErrExit]' If you get an error, find GetLastError on MSDN. There' you will find a link to a list of error codes.CallDLL#kernel32,"GetLastError", ret AsULongIf ret ThenPrint"Error ";ret
' Free the handles we created and exit.If hFile Then _
CallDLL#inet,"InternetCloseHandle", _
hFile AsULong, ret AsLongIf hInet Then _
CallDLL#inet,"InternetCloseHandle", _
hInet AsULong, ret AsLong' Strangely, the next line causes LB to hang for around 45' seconds on my computer, a 2.2 GHz running XP on dial-up.Close#inet
End'CODE ENDS
Retrieving HTTPS Data
by Brent Thorn -Originally in Liberty BASIC Newsletter #37
This demo reads a remote file into memory. Hopefully Yahoo doesn't mind my choice of URLs. Please note the strange hanging behavior at the "Close #inet" statement.
After doing a little testing, I found my hanging was the result of some ad-blocking software I have installed. When I disabled it the hanging ceased. It may be rare but you may want to keep this possibility in mind when using my code in your own programs. I can only suggest not closing #inet until after all your windows are closed at exit to avoid any possibility of a user's thinking your app is faulty.
Demo