Is it possible to check the existence of a URL in asp.net?

  • Thread starter Thread starter TaeHo Yoo
  • Start date Start date
T

TaeHo Yoo

The senario is we have a table that contains a number of URLs and we
want to check periodically that they exist. If not, then we will create
a report and send it to a admin.
So what I need to do is something like this,


---------------------------------------------------------------
run schedule task -- I have done it so don't worry about it

get URLs from DB

if any URLs don't exist then <<<<= HOW TO DO THIS? ANT EXAMPLE
CODE????

create a report and send it to admin

end of task
---------------------------------------------------------------


Thanks a lot in advance for your precious time.

All the best
 
Thanks Natty.
However, when I use the class like the following

-------------------------------------------------
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
myPage.Text = readHtmlPage("http://www.google.com/")
End Sub

Function readHtmlPage(url As String) As String
Dim objResponse As WebResponse
Dim objRequest As WebRequest
Dim result As String
objRequest = System.Net.HttpWebRequest.Create(url)
objResponse = objRequest.GetResponse()
Dim sr As New StreamReader(objResponse.GetResponseStream())
result = sr.ReadToEnd()

'clean up StreamReader
sr.Close()

return result
End Function
</script>
<html>
<body>
<b>This content is being populated from a separate HTTP request to
<a
href="http://aspalliance.com/stevesmith/">http://aspalliance.com/stevesm
ith/</a>:</b><hr/>
<asp:literal id="myPage" runat="server"/>
</body>
</html>

--------------------------------------------

I got stuck in the line
objResponse = objRequest.GetResponse()

what is wrong?
And the code above brings text from the www.google.com but it is wastful
since we just want to know whether this url exists or not.

Any idea?

Thanks
 
Instead of extracting the page as Natty suggests, try using the statuscode
property...

Dim httpReq As HttpWebRequest =
CType(WebRequest.Create("http://www.google.com"), HttpWebRequest)
httpReq.AllowAutoRedirect = False

Dim httpRes As HttpWebResponse = CType(httpReq.GetResponse(),
HttpWebResponse)

If httpRes.StatusCode = HttpStatusCode.OK Then
' your page exists.
End If

httpRes.Close()


--
Regards

John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional .NET for Java Developers with C#
ISBN:1-861007-91-4
Professional Windows Forms
ISBN: 1861005547
Professional JSP 2nd Edition
ISBN: 1861004958
Professional JSP
ISBN: 1861003625
Beginning JSP Web Development
ISBN: 1861002092
</shameless_author_plug>
 
Back
Top