Link to home
Start Free TrialLog in
Avatar of paulwhelan
paulwhelan

asked on

check if a website is 'live'

Hi

Is it possible to do this in VB?
I need to create a 'Job' that I can run with SQL Server and I think I might need to do that with a VB script.
Thanks


https://www.experts-exchange.com/questions/21871178/Auto-insert-and-check-if-website-is-live.html

I know that I can check if a website is 'live' by doing this in C#

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(websiteToRequest);
            webRequest.Timeout = 6000;
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
string responseStream = webResponse.GetResponseHeader("Content-Length");

But does anyone know how to set up a scheduled task to check if a site is 'live'
and then insert into a table in my sql server database to say if its live or not?

ASKER CERTIFIED SOLUTION
Avatar of mvidas
mvidas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops.. looks like my fingers got ahead of my, I put a 'v' in there:

' vwebfileexists = False
 WebFileExists = False

Sorry about that! Matt
There's always URLDownloadToFile...

Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Public Function DownloadFile(URL As String, LocalFilename As String) As Boolean
    Dim lngRetVal As Long
    lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
    If lngRetVal = 0 Then DownloadFile = True
End Function

Private Function IsOnline() As Boolean
    IsOnline = DownloadFile("http://yoursite.com", App.Path & "\check.html")
End Sub
Avatar of [ fanpages ]
[ fanpages ]

...or you could 'PING' the site domain...

"IcmpSendEcho: Ping a Machine by IP Address"
[ http://vbnet.mvps.org/index.html?code/internet/ping.htm]


BFN,

fp.
Avatar of paulwhelan

ASKER

Thanks Guys,

In Enterprise Manager it says
Select Job Commend Type
I select Active Script (for example VBScript or Javascript)

Then it says type the VBScript.

Do I just type


Function WebFileExists(ByVal vWebAddress)
 Dim oXMLHTTP, vWebText
 Set oXMLHTTP = CreateObject("msxml2.xmlhttp")
 oXMLHTTP.Open "GET", vWebAddress, False
 oXMLHTTP.send
 WebFileExists = False
 If oXMLHTTP.readyState = 4 And oXMLHTTP.Status = 200 Then WebFileExists = True
 Set oXMLHTTP = Nothing
 Set vWebText = Nothing
End Function

in there?

Also can I call the function in there?
If so where?
Do I just do
WebFileExists("https://www.experts-exchange.com")

And finally how do I update the table with this Job?
Thanks
Paul



For example I enter this

Function WebFileExists(ByVal vWebAddress)
 Dim oXMLHTTP, vWebText
 Set oXMLHTTP = CreateObject("msxml2.xmlhttp")
 oXMLHTTP.Open "GET", vWebAddress, False
 oXMLHTTP.send
 WebFileExists = False
 If oXMLHTTP.readyState = 4 And oXMLHTTP.Status = 200 Then WebFileExists = True
 Set oXMLHTTP = Nothing
 Set vWebText = Nothing
End Function

WebFileExists("www.yahoo.com");

and when I try to parse it says
'Expected end of statement"

thanks
PAul
Ok back to basics

I added a Job in Enterprise manager and this is the script

set FSO = server.createobject("Scripting.FileSystemObject")
set filewriter = FSO.createtextfile("d:/yourdirectory/yourfile.html")
filewriter.writeline "hello world"

set fso = nothing
set filewriter = nothing

And I set it to run every minute.

However
1 - nothing was created on my harddisk
2 - i cant seem to go back to edit or view the job?

thanks
Take your VBScript you pasted above & place it in a distinct file external to Enterprise Manager (and give it a ".vbs" extension).

Now set Enterprise Manager to run this .vbs file instead ("Execute Process Task" -> Set "Win32 Process" to be the full folder path/filename where you have saved the .vbs file).

Once you have this working we can look at re-coding the above suggestions in VBScript, rather than in VB(A) as they are shown in the previous comments.

BFN,

fp.
Hi Paul,

Putting just:
 WebFileExists("https://www.experts-exchange.com")
Won't do anything, since vbs needs to know what to do with that return value.  You could assign it to a variable, or even just msgbox it for testing.  Also, you may run into errors if you don't include the http:// beforehand.  Try pasting the following into your .vbs file like fp suggested or into the enterprise manager:

msgbox WebFileExists("https://www.experts-exchange.com")

Function WebFileExists(ByVal vWebAddress)
 Dim oXMLHTTP, vWebText
 Set oXMLHTTP = CreateObject("msxml2.xmlhttp")
 oXMLHTTP.Open "GET", vWebAddress, False
 oXMLHTTP.send
 vwebfileexists = False
 If oXMLHTTP.readyState = 4 And oXMLHTTP.Status = 200 Then WebFileExists = True
 Set oXMLHTTP = Nothing
 Set vWebText = Nothing
End Function

If you put it into a .vbs file, double click that .vbs file to run it after saving without scheduling it as a task.
Matt
It would be better to use MsgBox CStr(WebFileExists("https://www.experts-exchange.com")), as this converts the boolean value to a string containing "True" or "False". I know this is true of "proper" VB.

Other than that, mvidas has the right idea.
I think Matt was simply demonstrating the result, or how the function may be used , but yes, the MsgBox statement does expect a string data type as the parameter relating to the text message to display.  If any other data type is used, then the VBA interpreter will convert to a string prior to presentation.  This is also the case within Visual Basic (for Windows).

FYI: Further issues have been raised in another question:

"Adding a VB 'Job' in SQL Enterprise Manager"
[ https://www.experts-exchange.com/questions/21872209/Adding-a-VB-'Job'-in-SQL-Enterprise-Manager.html ]

BFN,

fp.
Hi guys

Sorry for delayed reply.

Where exactly is

"Execute Process Task"

for the command

"Execute Process Task" -> Set "Win32 Process"

I cant seem to find it in Enterprise Manager.

Thanks
Paul

Anyone?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Dan.  No objections from me.