Does url exist?

M

McKirahan

Henrik said:
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
 
A

Aaron [SQL Server MVP]

With Server.CreateObject("Microsoft.XMLHTTP")

FYI, this one isn't recommended for use from ASP... MSXML2.ServerXMLHTTP is
much safer.
 
S

Steven Burn

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!
 
J

Jonathan Dodds

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.)


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!

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!
 
M

McKirahan

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")
 
M

Mark Schupp

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

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!
 
S

Steven Burn

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!
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top