Reputation: 4663
I'm trying to run this vbscript in Internet Explorer, but it doesnt seem to work. It works when I make it .vbs file and double click, but not on the browser.
Dim strWebsite
strWebsite = "www.site.org"
If PingSite( strWebsite ) Then
WScript.Echo "Web site " & strWebsite & " is up and running!"
Else
WScript.Echo "Web site " & strWebsite & " is down!!!"
End If
Function PingSite( myWebsite )
Dim intStatus, objHTTP
Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
objHTTP.Open "GET", "http://" & myWebsite & "/", False
objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)"
On Error Resume Next
objHTTP.Send
intStatus = objHTTP.Status
On Error Goto 0
If intStatus = 200 Then
PingSite = True
Else
PingSite = False
End If
Set objHTTP = Nothing
End Function
What is the correct way to do that ?
Upvotes: 0
Views: 1465
Reputation:
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.3.0")
oXMLHTTP.Open "GET", "http://..................", False
oXMLHTTP.Send
If oXMLHTTP.Status = 200 Then
'"website ok"
End If
Upvotes: 2
Reputation: 14655
Hot from the MS press: VBScript is no longer supported in IE11 edge mode (as you probably knew, other browsers (that don't run on an MS renderer) didn't run vbs anyway)
The 'correct' way is to translate this to javascript (no seriously, you can currently still mock about but it's clearly very deprecated and advised to update old code now), which isn't that hard since this technique originated at MS. You'd still do a head-request and check the status-no.
This should get you started: HTTP HEAD Request in Javascript/Ajax?
EDIT (addressing your comment):
Don't count on reliably setting the useragent
though (that, like the above script, seems more a thing of the past):
EDIT2:
I see/think you want somehow to differentiate your app: MyApp 1.0;
(in the logs perhaps?). If that's the case, you might want to add some custom headers instead:
How can I add a custom HTTP header to ajax request with js or jQuery?
Also, most server-logs (by default) pick up on the GET
-String, you might want to use/add-to that (so you wouldn't have to change the log-format if your custom header wouldn't show up)?
Upvotes: 1