Load Web Page via Windows Form or Service

S

Scott Ocamb

I want to write a simple class that loads a requested url and reports
success or failure. I plan to use this to monitor web sites on the web.

I would like to implement the class in a windows form and a service.

Any help would be appreciated.

scott
 
J

Jared

You may be able to use the System.Net.WebClient to perform the task. But, if
you are running the code as a service make sure an account other than system
is specified or the service won't have access to resources off the machine.
HTH,
Jared

Shared Sub Main()
Console.WriteLine("Google: " &
CheckSite("http://www.google.com").ToString)
Console.WriteLine("MSN: " & CheckSite("http://www.msn.com").ToString)
Console.WriteLine("Bad URL: " &
CheckSite("http://www.thisisabadurl.com").ToString)
Console.ReadLine()
End Sub

Shared Function CheckSite(ByVal URL As String) As Boolean
Dim wcSiteStatus As New System.Net.WebClient
Dim stream As System.IO.Stream = New System.IO.MemoryStream
Dim reader As System.IO.StreamReader
Try
With wcSiteStatus
stream = .OpenRead(URL)
reader = New System.IO.StreamReader(stream)
If reader.Peek > 0 Then
Return True
Else
Return False
End If
End With
Catch ex As Exception
Return False
Finally
If Not reader Is Nothing Then reader.Close()
wcSiteStatus.Dispose()
End Try
End Function
 
J

Jared

I came across an example that is much better in this scenario. The idea came
from Duncan Mackenzie's "Digital Grandma" article in this months MSDN mag.
Jared

Shared Sub Main()
Console.WriteLine("Google: " & CheckSite("http://www.google.com"))
Console.WriteLine("MSN: " & CheckSite("http://www.msn.com"))
Console.WriteLine("Bad URL: " &
CheckSite("http://www.thisisabadurl.com"))
Console.ReadLine()
End Sub

Shared Function CheckSite(ByVal URL As String) As String
Try
Dim Request As Net.HttpWebRequest = _
Net.WebRequest.Create(URL)
Return CType(Request.GetResponse,
Net.HttpWebResponse).StatusCode.ToString
Catch webex As System.Net.WebException
Return webex.Status.ToString
Catch ex As Exception
Throw
End Try
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top