Simple code to verify a URL working

Pcolaboy

Member
Joined
Jun 29, 2007
Messages
6
Programming Experience
1-3
I'm a budding VB'er and I have been working on a project to easily monitor several of our web servers. It's a bit complicated, but I can't simply monitor SMNP traps for IIS - I have to verify that the page is reachable through a browser.

I would love to be able to design a simple app to display labels of all my servers on one form with a small indicator box to the side of each one that would be green or red indicating if the server's URL can be loaded successfully through a browser or not.

Here's a bit of layman's logic for what I want the real code to do:

1. Launch browser 'invisibly' (or some other method) directed to each of the individual server's URLs.

2. If the page loads successfully (I assume I'll have to look for a unique value on the page), then set the color property of that server's box object to green, else set the color property of that server's box object to red.

3. Refresh every 5 minutes

I should point out that each server also has a unique Keepalive.htm that is kept active.

I hope this makes sense to someone. I certainly appreciate any assistance that I receive !!! :)

Scott
 
See if this helps.
VB.NET:
Public Class Form1
	Dim UrlExists As Boolean = False

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim url As New Uri("http://www.google.com")
		MessageBox.Show("The url: " & url.ToString & IIf(DoesUrlExist(url), " does ", " does not ") & "exist")
	End Sub

	Public Function DoesUrlExist(ByVal Url As Uri) As Boolean
		Dim Req As Net.WebRequest
		Req = Net.WebRequest.Create(Url)
		Dim Resp As Net.WebResponse
		Try
			Resp = Req.GetResponse
			Return True
		Catch exc As Exception
			Return False
		End Try
	End Function
End Class
 
Thanks for the suggested code. It does work correctly.

What I really want to do is essentially have a form that displays a non-interactive button with the label of my server that I want to the code to evaluate or poll the URL every 10 minutes automatically and change the color of the button based upon the following logic:


If DoesURLExist(URL) true then change button1.backcolor to green
else change button1.backcolor to red

Wait 10 minutes and poll again.

Thanks again for any additional help you can provide!!!

Scott
 
Remember...I'm still a newbie...I've been playing around with the timer method and I got it to where it will work but only for one interval. In other words, I'm not sure how to use my existing code and loop it with the timer.

Any suggestions are still warmly welcomed :)
 
Make a new project, add a label and a timer to the form.
Double click the form background to enter the code.
Replace all code with code below:
VB.NET:
Public Class Form1
    Dim url As New Uri("http://www.google.com")

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 600000 '10 mins
        Timer1.Start() 'start timer ticking

        Timer1_Tick(Nothing, Nothing) 'timer wont tick for 10 mins, so tick it once now
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If DoesUrlExist(url) Then 'if site is working
            Label1.Text = "Working!"
        Else
            Label1.Text = "Not working!"
        End If
    End Sub

    Public Function DoesUrlExist(ByVal Url As Uri) As Boolean
        Dim Req As Net.WebRequest = Net.WebRequest.Create(Url)
        Dim Resp As Net.WebResponse
        Try
            Resp = Req.GetResponse
            Return True
        Catch exc As Exception
            Return False
        End Try
    End Function
End Class
 
Ford-P thanks very much for the code. I have used it but I've got a few bugs to work through. The main problem that I'm having is that it will only perform two sometimes three "loops" and then stop - based upon a timestamp label that I added at the end of the tick procedure. Keep in mind that I essentially want this app to run constantly until exited. The only user interaction would be to exit the program if desired.
Here is my code:

Public Class Form1
Inherits System.Windows.Forms.Form
Dim url As New Uri("http://servername/logon.asp")

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 5000 ' 5 seconds
Timer1.Start() 'start timer ticking
Timer1_Tick(Nothing, Nothing)
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'code below is for setting the buttons color as an indicator on the URL status
If DoesUrlExist(url) Then 'if site is working
button1.BackColor = System.Drawing.Color.Green
Else
button1.BackColor = System.Drawing.Color.Red
End If
label1.Text = DateTime.Now 'Put a timestamp in a label every cycle
End Sub

Public Function DoesUrlExist(ByVal url As Uri) As Boolean
Dim Req As Net.WebRequest = Net.WebRequest.Create(url)
Dim Resp As Net.WebResponse
Try
Resp = Req.GetResponse
Return True
Catch exc As Exception
Return False
End Try

End Function


End Class


Thanks again for all the fantastic help guys/gals.

Scott
 
Try using the following function:
VB.NET:
    Public Function DoesUrlExist(ByVal url As Uri) As Boolean
        Dim Req As Net.WebRequest = Net.WebRequest.Create(url)
        Dim Resp As Net.WebResponse = Nothing
        Try
            Resp = Req.GetResponse
            Return True
        Catch ex As Exception
            Return False
        Finally
            Resp.Close()
        End Try
    End Function

The "finally" code will execute whether it worked or not, and close the old connection. You may want to add a BackgroundWorker or threading as the program will become unresponsive for a couple of seconds when it gets the sites response. Hope that helps.
 
Hi guys,

The code works fine as long as the URL doesn't throw an exception (i.e. I purposely put in an erroneous address to make it fail).

Essentially, the code breaks at the RESP.CLOSE() line with the following error:
"An unhandled exception of type 'System.NullReferenceException' occurred in Project3.exe

Additional information: Object reference not set to an instance of an object.
"

Try using the following function:
VB.NET:
    Public Function DoesUrlExist(ByVal url As Uri) As Boolean
        Dim Req As Net.WebRequest = Net.WebRequest.Create(url)
        Dim Resp As Net.WebResponse = Nothing
        Try
            Resp = Req.GetResponse
            Return True
        Catch ex As Exception
            Return False
        Finally
            Resp.Close()
        End Try
    End Function

The "finally" code will execute whether it worked or not, and close the old connection. You may want to add a BackgroundWorker or threading as the program will become unresponsive for a couple of seconds when it gets the sites response. Hope that helps.
 
Back
Top