InternetOpenURL(), InternetReadFile() problem... download running away (wininet dll)
I have a program that downloads files from the web (automatically) and stores them. I do the downloading with the code below (give to me by someone else on this board!) and it works great... for no reason at all - sometimes the downloads just seem like they don't even try to DL and my program just goes through page after page (kinda like it runs away). Since these are compiled instances I can't see what's wrong... I'm trying to test it in the development environment to see what's up but it's not recreating the problem.
I am on a cable modem and I have multiple instances of the software working... it'll work fine (sometimes hours after starting, and others just minutes after starting) and then the program will just start running away and going through pages it needs to download... acting like they downloaded but they definately didn't.
I know I have give nothing to base a hypothesis of what's wrong on, but I was jsut wondering if anyone knows of an inherit problem with this code, something that the cable modem/ISP could do to trigger this behavior, or something I might be doing wrong that could cause this.
Thanks
VB Code:
'downloading for the 'download' bit
Option Explicit
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal sURL As String, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Private Const IF_FROM_CACHE = &H1000000
Private Const IF_MAKE_PERSISTENT = &H2000000
Private Const IF_NO_CACHE_WRITE = &H4000000
Private Const BUFFER_LEN = 256
Dim source As String
Public Function GetURLSource(sURL As String)
On Error GoTo ErrorHandler
Dim sBuffer As String * BUFFER_LEN, iResult As Integer, sData As String
Dim hInternet As Long, hSession As Long, lReturn As Long
hSession = InternetOpen("vb wininet", 1, vbNullString, vbNullString, 0)
If hSession Then hInternet = InternetOpenUrl(hSession, sURL, vbNullString, 0, IF_NO_CACHE_WRITE, 0)
If hInternet Then
iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
sData = sBuffer
Do While lReturn <> 0
iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
sData = sData + Mid(sBuffer, 1, lReturn)
Loop
End If
iResult = InternetCloseHandle(hInternet)
GetURLSource = sData
Exit Function
ErrorHandler:
'MsgBox "Sub/Function: GetURLSource" & vbCrLf & vbCrLf & "Encountered error #" & Err.Number & vbCrLf & Err.Description
Resume Next
End Function
PS: When I unplug my ethernet cable they all 'wait' and stop where they are... then when I plug it back in they resume... this is a great feature but it means that it isn't because the internet connection might be lost or timeout.