Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Handling Inet Errors in a Loop

Posted on 1999-07-23
5
Medium Priority
?
281 Views
Last Modified: 2008-02-01
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
Comment
Question by:James
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 

Author Comment

by:James
ID: 1526884
Edited text of question.
0
 
LVL 3

Expert Comment

by:jbil
ID: 1526885
'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
 

Author Comment

by:James
ID: 1526886
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
 
LVL 2

Accepted Solution

by:
Biffo earned 800 total points
ID: 1526887
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
 
LVL 2

Expert Comment

by:Biffo
ID: 1526888
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question