Getting+Connected+and+Online

//by Malcolm Marten, August 2007//

The simple method of downloading a web page "Downloading a file from a URL" can fail if you are not connected or not online.

While this sounds obvious, the nature of the Internet being a connected web makes this a little more complicated than you might think. The Windows environment has the concept of "Working Offline" so that previously downloaded content can be used locally. In "Offline" mode a connection can be "made" but it is only to your local machine, or perhaps the local network but not the actual Internet. So calls that you think are opening a network connection are in fact just accessing your local machine, and give 'success' results. Calls such as "URLDownloadToFileA" can subsequently fail although the connection is working correctly.

We need two API calls to clear up the situation and inform the user accordingly. The first is "InternetGetConnectedState", a most useful call that returns a TRUE if Windows thinks that there is a valid Internet connection route available. It does not actually try the connection so it is saying that it should work. If it returns FALSE then there is no discernible connection route.

The important information about whether you are Offline is returned in a bit set in the returned status flag that also gives other information. The flag can have a combination of the following bits set.

&40 INTERNET_CONNECTION_CONFIGURED Local system has a valid connection to the Internet, but it may or may not be currently connected.

&02 INTERNET_CONNECTION_LAN Local system uses a local area network to connect to the Internet.

&01 INTERNET_CONNECTION_MODEM Local system uses a modem to connect to the Internet.

&20 INTERNET_CONNECTION_OFFLINE Local system is in offline mode.

&04 INTERNET_CONNECTION_PROXY Local system uses a proxy server to connect to the Internet.

To get any new information from the Internet you must be online and connected. We can force the state to Online by using the second API call "InternetGoOnline". This will switch to Online mode, prompting the user if not already Online, and automatically call up the Dial Up Network if necessary to connect.

Here is a modified version from "Downloading a file from a URL" to show how this is all put together. Set Verbose% to FALSE if you do not want status reporting.

code format="bb4w" url$ = "http://www.bbcbasic.co.uk/bbcwin/bbcwin.html" file$ = "C:\bbcwin.html" PROCurldownloadflush(url$, file$, TRUE) END

DEF PROCurldownloadflush(url$, file$, Verbose%) LOCAL urlmon%, wininet%, res%, M%     SYS "LoadLibrary", "URLMON.DLL" TO urlmon% SYS "GetProcAddress", urlmon%, "URLDownloadToFileA" TO `URLDownloadToFile` SYS "LoadLibrary", "WININET.DLL" TO wininet% SYS "GetProcAddress", wininet%, "DeleteUrlCacheEntryA" TO `DeleteUrlCacheEntry` SYS "GetProcAddress", wininet%, "InternetGetConnectedState" TO `InternetGetConnectedState` SYS "GetProcAddress", wininet%, "InternetGoOnline" TO `InternetGoOnline` SYS `InternetGetConnectedState`, ^M%,0 TO res% IF Verbose% THEN IF (M% AND &20) PRINT" Currently working Offline." IF res%=0 PRINT "No active Internet connection is present." PRINT"Trying to Connect to "+url$ ENDIF SYS `InternetGoOnline`, url$, @hwnd% ,0 TO res% IF res%=0 ERROR 100, "No Internet connection could be established!" SYS `DeleteUrlCacheEntry`, url$ SYS `URLDownloadToFile`, 0, url$, file$, 0, 0 TO res% IF res% ERROR 100, "Couldn't download "+url$ ELSE IF Verbose% PRINT "File downloaded to "+file$ ENDPROC

code