Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for WWW connectivity...

Status
Not open for further replies.

JPJeffery

Technical User
May 26, 2006
600
0
0
GB
I'm sure this code used to work but I can't force a false positive.

In summary, the code opens IE and navigates to a URL. The If...ElseIf..EndIf loop (immediately after the [tt]wscript.echo objIE.Document.Body.innerHTML[/tt] line, which - and this MUST be a clue - produces no output) is meant to check the contents of the resulting page to see if the connection was established.

The first two tests in that ElseIf allow for different versions of IE using different wordings to their error pages, one of which should have picked up the contents of a local html file saved from a forced error page (by removing the network cable, surfing to a URL and doing Save As!). The third test checks for spaces in the code which really ought to flag up an error under ANY conditions!!

Code:
Sub CheckInternet(strTestURL)
    Dim strInternetError
    If gbDebug = True then
        wscript.echo "18"
    End If
    Set objIE = wscript.CreateObject("InternetExplorer.Application","objIE_")
    If Err.Number <> 0 Then
        On Error GoTo 0
        Msg = "IE application not found."
        MsgBox Msg,vbExclamation,strAppDescription
        Wscript.Quit
    End If
    On Error GoTo 0

    objIE.visible = false 'true
    objIE.ToolBar = 0
    objIE.statusbar=false
    objIE.Navigate "about:blank"

    Do Until objIE.ReadyState = 4
        WScript.Sleep 100
    Loop

    objIE.Navigate strTestURL

    Do While objIE.Busy = True
        WScript.Sleep 500
    Loop

    ' [URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=1383115&page=1[/URL] refers to
    ' following loops
    'WScript.Sleep 500
    do while objIE.Document is nothing
        wscript.sleep 50
    loop
    do while objIE.Document.body is nothing
        wscript.sleep 50
    loop
    wscript.echo objIE.Document.Body.innerHTML
    If InStr(objIE.Document.Body.innerHTML, "The Web site might be experiencing technical difficulties") > 0 then
        wscript.echo "  Unable to connect to " & strTestURL & "."
        BuildTable(conRed & "Unable to connect to " & strTestURL & conBlack)
    ElseIf InStr(objIE.Document.Body.innerHTML, "Internet Explorer cannot display the webpage") > 0 Then
        wscript.echo "  Unable to connect to " & strTestURL & "."
        BuildTable(conRed & "Unable to connect to " & strTestURL & conBlack)
    ElseIf InStr(objIE.Document.Body.innerHTML, " ") > 0 Then
        wscript.echo "  Unable to connect to " & strTestURL & "."
        BuildTable(conRed & "Unable to connect to " & strTestURL & conBlack)
    Else
        strInternetError=False
        wscript.echo "  Tested connectivity to " & strTestURL & " OK."
        BuildTable("Connection OK to " & strTestURL)
    End If
    objIE.quit

    Set ObjIE = Nothing
End Sub

What needs to be changed?

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
*bump*

Any ideas about this?

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
Okay, gave this a shot, I changed the loops between the '----------- comments, just my preference, since it wasn't waiting on my tests...

Code:
Sub CheckInternet(strTestURL)
    Dim strInternetError
    If gbDebug = True then
        wscript.echo "18"
    End If
    Set objIE = wscript.CreateObject("InternetExplorer.Application","objIE_")
    If Err.Number <> 0 Then
        On Error GoTo 0
        Msg = "IE application not found."
        MsgBox Msg,vbExclamation,strAppDescription
        Wscript.Quit
    End If
    On Error GoTo 0

    objIE.visible = false 'true
    objIE.ToolBar = 0
    objIE.statusbar=false
    objIE.Navigate "about:blank"

    Do Until objIE.ReadyState = 4
        WScript.Sleep 100
    Loop

    objIE.Navigate strTestURL

    Do While objIE.Busy = True
        WScript.Sleep 500
    Loop

    ' [URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=1383115&page=1[/URL] refers to
    ' following loops
    'WScript.Sleep 500
'-----------------------------------------------------------	
	'	wait for the page to be fully loaded...
	do until objIE.readystate = 4
		WScript.Sleep 500
	loop
	
	do until not objIE.busy
		WScript.Sleep 500
	loop
'-----------------------------------------------------------
	
    wscript.echo mid(objIE.Document.Body.innerHTML, 1, 50)
    If InStr(objIE.Document.Body.innerHTML, "The Web site might be experiencing technical difficulties") > 0 then
        wscript.echo "  1Unable to connect to " & strTestURL & "."
'        BuildTable(conRed & "Unable to connect to " & strTestURL & conBlack)
    ElseIf InStr(objIE.Document.Body.innerHTML, "Internet Explorer cannot display the webpage") > 0 Then
        wscript.echo "  2Unable to connect to " & strTestURL & "."
'        BuildTable(conRed & "Unable to connect to " & strTestURL & conBlack)
    ElseIf InStr(objIE.Document.Body.innerHTML, " ") > 0 Then
        wscript.echo "  3Unable to connect to " & strTestURL & "."
'        BuildTable(conRed & "Unable to connect to " & strTestURL & conBlack)
    Else
        strInternetError=False
        wscript.echo "  4Tested connectivity to " & strTestURL & " OK."
'        BuildTable("Connection OK to " & strTestURL)
    End If
    objIE.quit

    Set ObjIE = Nothing
End Sub

dim url

url = "[URL unfurl="true"]http://www.msn.com"[/URL]
CheckInternet(url)

And this comes out as #3 (I put numbers on the .echos just so I knew where/what was going on.

Is that what you wanted ?

>
 
Well, no, not really. With these changes my script just hangs!
:)

I've thought of a different approach to get the InStr test to work though. Save the webpage to a .txt/.html file and run the InStr test on that (rather than the slightly more 'amorphous' innerHTML object property).

But, I don't know how to perform that file save...can someone tell me how to do this?

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
That part's easy...

Code:
const ForReading = 1, ForWriting = 2
dim fso, daHTML, outfile

daHTML = objIE.document.documentElement.outerHTML

'	create the FSO and output file objects
Set fso = CreateObject("Scripting.FileSystemObject")
Set outfile = fso.CreateTextFile([COLOR=red]"C:\MyFile.html"[/color], True)  ' overwrite = True

'	write out the new file and close it up...	
outfile.write(daHTML)
outfile.Close()

'	close all the object handles...
Set fso = Nothing
Set outfile = Nothing

msgbox "All Done",,"VBScript"

There's a few intermediate steps you might not want to take but you get the overall idea.

Strange my version hangs, and your version seemed to zip right past the loops to wait for the page.


>






 
Well, changing
Code:
objIE.visible = false
to
Code:
objIE.visible = true
has demonstrated that the .navigate method to a proper URL fails.

Curious...and perhaps somewhat depressing. Why does it not even seem to be trying to connect to (or wherever)?

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
???

I can see Bronx Mowgli on MSN just fine :)

Must be your PC.


>
 
You win the prize. The script runs fine on other PCs.

Well, except for the Instr tests, which always produce 0 (zero) even if the test web page is one which includes the text being searched for!

JJ
[small][purple]Variables won't. Constants aren't[/purple]
There is no apostrophe in the plural of PC (or PST, or CPU, or HDD, or FDD, and so on)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top