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

Discussion in 'ASP .Net' started by TaeHo Yoo, Oct 17, 2003.

  1. TaeHo Yoo

    TaeHo Yoo Guest

    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



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    TaeHo Yoo, Oct 17, 2003
    #1
    1. Advertising

  2. TaeHo Yoo

    Natty Gur Guest

    Natty Gur, Oct 19, 2003
    #2
    1. Advertising

  3. TaeHo Yoo

    TaeHo Yoo Guest

    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






    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    TaeHo Yoo, Oct 20, 2003
    #3
  4. 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>
    ----------------------------------------------

    "TaeHo Yoo" <> wrote in message
    news:...
    > 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
    >
    >
    >
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    John Timney \(Microsoft MVP\), Oct 20, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leon
    Replies:
    1
    Views:
    437
    John Timney \(Microsoft MVP\)
    Oct 18, 2003
  2. CK
    Replies:
    5
    Views:
    24,197
    Lars-Inge T√łnnessen \(VJ# MVP\)
    Mar 21, 2006
  3. Replies:
    3
    Views:
    2,959
    Gordon Beaton
    Apr 17, 2006
  4. Scott
    Replies:
    2
    Views:
    288
    Scott
    Sep 23, 2005
  5. How to check URL existence ?

    , Aug 24, 2006, in forum: Javascript
    Replies:
    25
    Views:
    320
Loading...

Share This Page