• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Handling Inet Errors in a Loop

Hi!

  I need help in handling Inet when retreiving web pages in a loop. Some URLs cause Inet to hang because it cannot connect and my loop just stalls :( On Error Resume Next is no help because once it resumes it just races through my URLs without saving them to file. HELP!


                Open App.Path & "\links.dat" For Input As #1
                  Do While Not EOF(1)
                  Line Input #1, strURL
                  DoEvents
                Open App.Path i & ".htm" For Output As #2
                  strFILE = Inet1.OpenURL(strURL, 0)
                  Print #2, strFILE
                  Close #2
                Loop
                Close #1
0
James
Asked:
James
  • 2
  • 2
1 Solution
 
JamesAuthor Commented:
Edited text of question.
0
 
jbilCommented:
'Try this, after the line below.....

'Open App.Path i & ".htm" For Output As #2
'use these lines

Do While Inet1.StillExecuting = True

Loop

If Len(strFILE) > 1 Then
Print #2, strFILE
Close #2
ELSE
Close #2
End If

strFILE=""

Loop
Close #1

0
 
JamesAuthor Commented:
jbil,


 same thing. It seems it just can't get itself out of the loop and on to the next url from the url file. You can trap for the errors and say On Error GoTo Line: but for some reason passing a strURL gets all screwed up and nothing really happens after it continus from next line. This is really odd but I am sure it has to do with the fact I am in a Loop.

Here is a bad url that will cause unspeakable confsion to inet if you want to observe this behaviour first hand, place on top of some good url links in a file and run the code.

http://crinos.stanford.edu/~jimge/resume.html

0
 
BiffoCommented:
Here is what the problem is....

Inet is a piece of ****, it is best used for retrieving known working single url's, such as your own that you know is valid. The guy's at MS who build this crap never use the stuff in the real world under real world conditions LOL They test it under ideal conditions using idea url addresses <snicker>

The gist of your problem has to do with whoever built the Inet control at MS, they thought it be cute to have it triger not one, but two friggin error events. This makes handling errors impossible for all intent and  purposes. So, when you do a "On Error Resume Next" it responds to one error event, to yet another following the first one -- right after another and it just get's compounded in a loop. Real messy business.

A more stable way of doing what you want is to use the wininet stuff. First, get the wininet API from the following address:

http://support.microsoft.com/support/kb/articles/Q185/5/19.ASP

It comes as vbinet.exe I think. Add that API module (wininet.bas) to your code.

Now just modifiy your code to resemble this:

Private Sub Whatever_it_is()

Open App.Path & "\links.dat" For Input As #1
Do While Not EOF(1)
Line Input #1, strURL
DoEvents

Call Do_WinInet


Loop
Close #1
End Sub

Private Sub Do_WinInet()
On Error Resume Next
    Dim hOpen               As Long
    Dim hOpenUrl            As Long
    Dim sUrl                As String
    Dim bDoLoop             As Boolean
    Dim bRet                As Boolean
    Dim sReadBuffer         As String * 2048
    Dim lNumberOfBytesRead  As Long
    Dim sBuffer             As String
    Dim iRetVal             As Integer

   hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
   'InternetSetStatusCallback hOpen, AddressOf MyCallBack
   
   hOpenUrl = InternetOpenUrl(hOpen, urlname, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
   
    bDoLoop = True
    While bDoLoop
    sReadBuffer = vbNullString
    bRet = InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
        sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)
        If Not CBool(lNumberOfBytesRead) Then bDoLoop = False
    Wend
   
 Open App.Path i & ".htm" For Binary Access Write As #2

    Put #2, , sBuffer
    Close #2

    If hOpen <> 0 Then InternetCloseHandle (hOpen)
    If hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl)

End Sub



This runs much better under VB5 then VB6 since VB6 has a lot of problems dealing with async issues and threading. Under VB6 your app will be completely unavailable till the downloading is complete. Under VB5 it is a little more forgiving, plus, you can run it as ASYNC if you want to put some effort into it. I run it this way with no problems. In VB6 forget it.
0
 
BiffoCommented:
One more thing, the line below I used just the name urlname to signify whatever the name of the url string you are using, in your case it is strURL, so replace urlname with that. I wasn't paying attention when I wrote this.


hOpenUrl = InternetOpenUrl(hOpen, strURL, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now