Does url exist?

Discussion in 'ASP General' started by Henrik, Mar 16, 2005.

  1. Henrik

    Henrik Guest

    Hi

    How do I find out if an url exists? Whar I want is a function like this:

    If UrlExists("http://www.test.com/test.html") Then
    Response.Write("Yeah!!")
    Else
    Response.Write("No file :-(")
    End If

    Can anyone help?

    /Henrik
    Henrik, Mar 16, 2005
    #1
    1. Advertising

  2. http://www.aspfaq.com/2173

    --
    Please post DDL, sample data and desired results.
    See http://www.aspfaq.com/5006 for info.




    "Henrik" <> wrote in message
    news:42384e50$0$24082$...
    > Hi
    >
    > How do I find out if an url exists? Whar I want is a function like this:
    >
    > If UrlExists("http://www.test.com/test.html") Then
    > Response.Write("Yeah!!")
    > Else
    > Response.Write("No file :-(")
    > End If
    >
    > Can anyone help?
    >
    > /Henrik
    >
    >
    Aaron [SQL Server MVP], Mar 16, 2005
    #2
    1. Advertising

  3. Henrik

    McKirahan Guest

    "Henrik" <> wrote in message
    news:42384e50$0$24082$...
    > Hi
    >
    > How do I find out if an url exists? Whar I want is a function like this:
    >
    > If UrlExists("http://www.test.com/test.html") Then
    > Response.Write("Yeah!!")
    > Else
    > Response.Write("No file :-(")
    > End If
    >
    > Can anyone help?
    >
    > /Henrik



    Will this help? Watch for word-wrap.

    Function UrlExists(URL)
    On Error Resume Next
    Err.Clear
    Dim b
    With Server.CreateObject("Microsoft.XMLHTTP")
    .Open "GET",URL,False
    .Send
    b = .ResponseBody
    If Err.Number <> 0 Or .Status <> 200 Then
    Fetch = False
    Exit Function
    End If
    End With
    Fetch = Err.Number = 0
    End Function


    I think that the object may vary by Windows version:

    CreateObject("Microsoft.XMLHTTP")
    CreateObject("MSXML2.ServerXMLHTTP")
    CreateObject("MSXML2.ServerXMLHTTP.3.0")


    So perhaps the following will work (untested):

    Function UrlExists(URL)
    On Error Resume Next
    Err.Clear
    Dim booXML
    booXML= False
    Dim strXML
    Dim objXML
    Set objXML = Server.CreateObject("Microsoft.XMLHTTP")
    If Err.Number = 0 Then
    booXML = True
    Else
    Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
    If Err.Number = 0 Then
    booXML = True
    Else
    Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    If Err.Number = 0 Then
    booXML = True
    End If
    End If
    Err.Clear
    End If
    objXML.Open "GET",URL,False
    objXML.Send
    strXML = .ResponseText
    If Err.Number <> 0 Or objXML.Status <> 200 Then
    Fetch = False
    Exit Function
    End If
    Fetch = Err.Number = 0
    End Function
    McKirahan, Mar 16, 2005
    #3
  4. > With Server.CreateObject("Microsoft.XMLHTTP")

    FYI, this one isn't recommended for use from ASP... MSXML2.ServerXMLHTTP is
    much safer.
    Aaron [SQL Server MVP], Mar 16, 2005
    #4
  5. Henrik

    Steven Burn Guest

    Here you go.......

    example:

    http://mysteryfcm.plus.com/misc/urlexists.asp

    Code:

    http://mysteryfcm.plus.com/misc/urlexists.asp.txt

    --
    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    Keeping it FREE!

    "Henrik" <> wrote in message news:42384e50$0$24082$...
    > Hi
    >
    > How do I find out if an url exists? Whar I want is a function like this:
    >
    > If UrlExists("http://www.test.com/test.html") Then
    > Response.Write("Yeah!!")
    > Else
    > Response.Write("No file :-(")
    > End If
    >
    > Can anyone help?
    >
    > /Henrik
    >
    >
    Steven Burn, Mar 16, 2005
    #5
  6. Henrik

    Steven Burn Guest

    Tis a modified version of one of the samples provided in Aaron's link btw.....

    --
    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    Keeping it FREE!

    "Steven Burn" <> wrote in message news:#...
    Here you go.......

    example:

    http://mysteryfcm.plus.com/misc/urlexists.asp

    Code:

    http://mysteryfcm.plus.com/misc/urlexists.asp.txt

    --
    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    Keeping it FREE!

    "Henrik" <> wrote in message news:42384e50$0$24082$...
    > Hi
    >
    > How do I find out if an url exists? Whar I want is a function like this:
    >
    > If UrlExists("http://www.test.com/test.html") Then
    > Response.Write("Yeah!!")
    > Else
    > Response.Write("No file :-(")
    > End If
    >
    > Can anyone help?
    >
    > /Henrik
    >
    >
    Steven Burn, Mar 16, 2005
    #6
  7. Henrik

    Steven Burn Guest

    Am I missing something here....... this doesn't return a value either way?. Where is the code telling the function to pass Fetch to URLExists? (tried adding that part myself btw and it still didn't return a value....)

    --
    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    Keeping it FREE!

    "McKirahan" <> wrote in message news:...
    > "Henrik" <> wrote in message
    > news:42384e50$0$24082$...
    > > Hi
    > >
    > > How do I find out if an url exists? Whar I want is a function like this:
    > >
    > > If UrlExists("http://www.test.com/test.html") Then
    > > Response.Write("Yeah!!")
    > > Else
    > > Response.Write("No file :-(")
    > > End If
    > >
    > > Can anyone help?
    > >
    > > /Henrik

    >
    >
    > Will this help? Watch for word-wrap.
    >
    > Function UrlExists(URL)
    > On Error Resume Next
    > Err.Clear
    > Dim b
    > With Server.CreateObject("Microsoft.XMLHTTP")
    > .Open "GET",URL,False
    > .Send
    > b = .ResponseBody
    > If Err.Number <> 0 Or .Status <> 200 Then
    > Fetch = False
    > Exit Function
    > End If
    > End With
    > Fetch = Err.Number = 0
    > End Function
    >
    >
    > I think that the object may vary by Windows version:
    >
    > CreateObject("Microsoft.XMLHTTP")
    > CreateObject("MSXML2.ServerXMLHTTP")
    > CreateObject("MSXML2.ServerXMLHTTP.3.0")
    >
    >
    > So perhaps the following will work (untested):
    >
    > Function UrlExists(URL)
    > On Error Resume Next
    > Err.Clear
    > Dim booXML
    > booXML= False
    > Dim strXML
    > Dim objXML
    > Set objXML = Server.CreateObject("Microsoft.XMLHTTP")
    > If Err.Number = 0 Then
    > booXML = True
    > Else
    > Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
    > If Err.Number = 0 Then
    > booXML = True
    > Else
    > Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    > If Err.Number = 0 Then
    > booXML = True
    > End If
    > End If
    > Err.Clear
    > End If
    > objXML.Open "GET",URL,False
    > objXML.Send
    > strXML = .ResponseText
    > If Err.Number <> 0 Or objXML.Status <> 200 Then
    > Fetch = False
    > Exit Function
    > End If
    > Fetch = Err.Number = 0
    > End Function
    >
    >
    Steven Burn, Mar 16, 2005
    #7
  8. In the xmlhttp.open call consider changing the HTTP method from GET to HEAD.

    The HEAD method will return only the headers, not the whole document. i.e.
    HEAD is faster than GET. Since the point is only to check for the existence
    of some resource at the specified URL, HEAD is sufficient.

    (What's the HEAD method for? Browsers and proxy servers can use the HEAD
    method to check if a cached page has been updated.)


    "Steven Burn" <> wrote in message
    news:...
    Tis a modified version of one of the samples provided in Aaron's link
    btw.....

    --
    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    Keeping it FREE!

    "Steven Burn" <> wrote in message
    news:#...
    Here you go.......

    example:

    http://mysteryfcm.plus.com/misc/urlexists.asp

    Code:

    http://mysteryfcm.plus.com/misc/urlexists.asp.txt

    --
    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    Keeping it FREE!

    "Henrik" <> wrote in message
    news:42384e50$0$24082$...
    > Hi
    >
    > How do I find out if an url exists? Whar I want is a function like this:
    >
    > If UrlExists("http://www.test.com/test.html") Then
    > Response.Write("Yeah!!")
    > Else
    > Response.Write("No file :-(")
    > End If
    >
    > Can anyone help?
    >
    > /Henrik
    >
    >
    Jonathan Dodds, Mar 17, 2005
    #8
  9. Henrik

    Steven Burn Guest

    Duly changed.... ;o)

    --
    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    Keeping it FREE!

    "Jonathan Dodds" <NO_REPLY> wrote in message news:...
    > In the xmlhttp.open call consider changing the HTTP method from GET to HEAD.
    >
    > The HEAD method will return only the headers, not the whole document. i.e.
    > HEAD is faster than GET. Since the point is only to check for the existence
    > of some resource at the specified URL, HEAD is sufficient.
    >
    > (What's the HEAD method for? Browsers and proxy servers can use the HEAD
    > method to check if a cached page has been updated.)
    >
    >
    > "Steven Burn" <> wrote in message
    > news:...
    > Tis a modified version of one of the samples provided in Aaron's link
    > btw.....
    >
    > --
    > Regards
    >
    > Steven Burn
    > Ur I.T. Mate Group
    > www.it-mate.co.uk
    >
    > Keeping it FREE!
    >
    > "Steven Burn" <> wrote in message
    > news:#...
    > Here you go.......
    >
    > example:
    >
    > http://mysteryfcm.plus.com/misc/urlexists.asp
    >
    > Code:
    >
    > http://mysteryfcm.plus.com/misc/urlexists.asp.txt
    >
    > --
    > Regards
    >
    > Steven Burn
    > Ur I.T. Mate Group
    > www.it-mate.co.uk
    >
    > Keeping it FREE!
    >
    > "Henrik" <> wrote in message
    > news:42384e50$0$24082$...
    > > Hi
    > >
    > > How do I find out if an url exists? Whar I want is a function like this:
    > >
    > > If UrlExists("http://www.test.com/test.html") Then
    > > Response.Write("Yeah!!")
    > > Else
    > > Response.Write("No file :-(")
    > > End If
    > >
    > > Can anyone help?
    > >
    > > /Henrik
    > >
    > >

    >
    >
    >
    Steven Burn, Mar 17, 2005
    #9
  10. Henrik

    McKirahan Guest

    "Steven Burn" <> wrote in message
    news:eqek$...
    Am I missing something here....... this doesn't return a value either way?.
    Where is the code telling the function to pass Fetch to URLExists? (tried
    adding that part myself btw and it still didn't return a value....)

    --
    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    [snip]

    Yeah, me bad.

    I renamed the function without renaming it within itself.

    Try this:

    If UrlExists("http://www.test.com/test.html") Then
    Response.Write("Yeah!!")
    Else
    Response.Write("No file :-(")
    End If

    Function UrlExists(xURL)
    On Error Resume Next
    Err.Clear
    With CreateObject("MSXML2.ServerXMLHTTP")
    .Open "HEAD",xURL,False
    .Send
    If Err.Number <> 0 Or .Status <> 200 Then
    UrlExists = False
    Exit Function
    End If
    End With
    UrlExists = Err.Number = 0
    End Function

    Thanks to:
    Aaron (re "MSXML2.ServerXMLHTTP")
    Jonathan (re "HEAD")
    McKirahan, Mar 17, 2005
    #10
  11. Henrik

    Mark Schupp Guest

    I think that he renamed another function when creating the example for you.
    Change "Fetch" to "UrlExists"

    --
    --Mark Schupp
    Head of Development
    Integrity eLearning
    www.ielearning.com

    "Steven Burn" <> wrote in message
    news:eqek$...
    Am I missing something here....... this doesn't return a value either way?.
    Where is the code telling the function to pass Fetch to URLExists? (tried
    adding that part myself btw and it still didn't return a value....)

    --
    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    Keeping it FREE!

    "McKirahan" <> wrote in message
    news:...
    > "Henrik" <> wrote in message
    > news:42384e50$0$24082$...
    > > Hi
    > >
    > > How do I find out if an url exists? Whar I want is a function like this:
    > >
    > > If UrlExists("http://www.test.com/test.html") Then
    > > Response.Write("Yeah!!")
    > > Else
    > > Response.Write("No file :-(")
    > > End If
    > >
    > > Can anyone help?
    > >
    > > /Henrik

    >
    >
    > Will this help? Watch for word-wrap.
    >
    > Function UrlExists(URL)
    > On Error Resume Next
    > Err.Clear
    > Dim b
    > With Server.CreateObject("Microsoft.XMLHTTP")
    > .Open "GET",URL,False
    > .Send
    > b = .ResponseBody
    > If Err.Number <> 0 Or .Status <> 200 Then
    > Fetch = False
    > Exit Function
    > End If
    > End With
    > Fetch = Err.Number = 0
    > End Function
    >
    >
    > I think that the object may vary by Windows version:
    >
    > CreateObject("Microsoft.XMLHTTP")
    > CreateObject("MSXML2.ServerXMLHTTP")
    > CreateObject("MSXML2.ServerXMLHTTP.3.0")
    >
    >
    > So perhaps the following will work (untested):
    >
    > Function UrlExists(URL)
    > On Error Resume Next
    > Err.Clear
    > Dim booXML
    > booXML= False
    > Dim strXML
    > Dim objXML
    > Set objXML = Server.CreateObject("Microsoft.XMLHTTP")
    > If Err.Number = 0 Then
    > booXML = True
    > Else
    > Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
    > If Err.Number = 0 Then
    > booXML = True
    > Else
    > Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    > If Err.Number = 0 Then
    > booXML = True
    > End If
    > End If
    > Err.Clear
    > End If
    > objXML.Open "GET",URL,False
    > objXML.Send
    > strXML = .ResponseText
    > If Err.Number <> 0 Or objXML.Status <> 200 Then
    > Fetch = False
    > Exit Function
    > End If
    > Fetch = Err.Number = 0
    > End Function
    >
    >
    Mark Schupp, Mar 17, 2005
    #11
  12. Henrik

    Steven Burn Guest

    I tried that before mentioning it and it still wouldn't work on this PC (as I mentioned, it didn't return a value either way, nor did it return an error)..... I basically had to strip out all of the If's/Then's for the different XMLHTTP versions and just leave MSXML2.ServerXMLHTTP for it to work.....

    --
    Regards

    Steven Burn
    Ur I.T. Mate Group
    www.it-mate.co.uk

    Keeping it FREE!

    "Mark Schupp" <> wrote in message news:ePlO#...
    > I think that he renamed another function when creating the example for you.
    > Change "Fetch" to "UrlExists"
    >
    > --
    > --Mark Schupp
    > Head of Development
    > Integrity eLearning
    > www.ielearning.com
    >
    > "Steven Burn" <> wrote in message
    > news:eqek$...
    > Am I missing something here....... this doesn't return a value either way?.
    > Where is the code telling the function to pass Fetch to URLExists? (tried
    > adding that part myself btw and it still didn't return a value....)
    >
    > --
    > Regards
    >
    > Steven Burn
    > Ur I.T. Mate Group
    > www.it-mate.co.uk
    >
    > Keeping it FREE!
    >
    > "McKirahan" <> wrote in message
    > news:...
    > > "Henrik" <> wrote in message
    > > news:42384e50$0$24082$...
    > > > Hi
    > > >
    > > > How do I find out if an url exists? Whar I want is a function like this:
    > > >
    > > > If UrlExists("http://www.test.com/test.html") Then
    > > > Response.Write("Yeah!!")
    > > > Else
    > > > Response.Write("No file :-(")
    > > > End If
    > > >
    > > > Can anyone help?
    > > >
    > > > /Henrik

    > >
    > >
    > > Will this help? Watch for word-wrap.
    > >
    > > Function UrlExists(URL)
    > > On Error Resume Next
    > > Err.Clear
    > > Dim b
    > > With Server.CreateObject("Microsoft.XMLHTTP")
    > > .Open "GET",URL,False
    > > .Send
    > > b = .ResponseBody
    > > If Err.Number <> 0 Or .Status <> 200 Then
    > > Fetch = False
    > > Exit Function
    > > End If
    > > End With
    > > Fetch = Err.Number = 0
    > > End Function
    > >
    > >
    > > I think that the object may vary by Windows version:
    > >
    > > CreateObject("Microsoft.XMLHTTP")
    > > CreateObject("MSXML2.ServerXMLHTTP")
    > > CreateObject("MSXML2.ServerXMLHTTP.3.0")
    > >
    > >
    > > So perhaps the following will work (untested):
    > >
    > > Function UrlExists(URL)
    > > On Error Resume Next
    > > Err.Clear
    > > Dim booXML
    > > booXML= False
    > > Dim strXML
    > > Dim objXML
    > > Set objXML = Server.CreateObject("Microsoft.XMLHTTP")
    > > If Err.Number = 0 Then
    > > booXML = True
    > > Else
    > > Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP")
    > > If Err.Number = 0 Then
    > > booXML = True
    > > Else
    > > Set objXML = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
    > > If Err.Number = 0 Then
    > > booXML = True
    > > End If
    > > End If
    > > Err.Clear
    > > End If
    > > objXML.Open "GET",URL,False
    > > objXML.Send
    > > strXML = .ResponseText
    > > If Err.Number <> 0 Or objXML.Status <> 200 Then
    > > Fetch = False
    > > Exit Function
    > > End If
    > > Fetch = Err.Number = 0
    > > End Function
    > >
    > >

    >
    >
    Steven Burn, Mar 17, 2005
    #12
    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. Poppy

    Does file exist using URL ??

    Poppy, Jun 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    468
    Patrice
    Jun 28, 2004
  2. Bill Johnson
    Replies:
    0
    Views:
    1,225
    Bill Johnson
    Jul 8, 2005
  3. Y.S.
    Replies:
    3
    Views:
    985
    strajan
    Sep 17, 2003
  4. LT
    Replies:
    7
    Views:
    2,078
    Phlip
    Jul 25, 2004
  5. Markus Mohr
    Replies:
    7
    Views:
    229
    Thomas 'PointedEars' Lahn
    Nov 28, 2003
Loading...

Share This Page