As a web site developer, I sometimes need to check if a specific file exists on a remote server. That is easily done with the ServerXMLHTTP object. As explained by Microsoft MSDN, it retrieves XML responses from a server. If you use the object to read the head of a specific url and then check its status, you will be able to determine the file existence.
Let's try to write the code!
First of all we create the object:
<% Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
Then we open it and send the request:
xmlhttp.open "HEAD", "http://www.someremotedomain.com/somefile.xxx", False
xmlhttp.send
As you can see, when opening, we use "HEAD" as request method. This is valid for IE7+ and it is used because we do not really need to get all the content (the "GET" request method will retrieve all of it). We just want to get the head. Please note that you need to change the site url.Now what we really are looking for is the request status. Checking the status number, we determine if the file exists:
Select Case Cint(xmlhttp.status)
Case 200, 202, 302
Set xmlhttp = Nothing
response.write ("File exists")
Case Else
Set xmlhttp = Nothing
response.write ("Not found")
End Select
%>
We are checking for status- 200 - OK (standard successful http request),
- 202 - Accepted (request accepted for processing, but not completed),
- 302 - Found (via redirection).
Here is the complete code:
<%
Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "HEAD", "http://www.someremotedomain.com/somefile.xxx", False
xmlhttp.send
Select Case Cint(xmlhttp.status)
Case 200, 202, 302
Set xmlhttp = Nothing
response.write ("File exists")
Case Else
Set xmlhttp = Nothing
response.write ("Not found")
End Select
%>
Slight problem exists, when you're looking at a website that you don't have control over, and that website is configured to give user-friendly html pages back for 404-not found errors. When this happens, you'll end-up incorrectly getting a readystate=4 and status=200 found.
ReplyDeleteAnd if you then believe this result, and say, try to download a ZIP file, you'll get a corrupt zip file, because its actually filled with html-text.
Exactly what I was after, thank you.
ReplyDeleteYou're welcome!
DeleteHow would you check to see if a remote image is valid without download it?
ReplyDeleteThank you, just what I needed to filter out bogus picture links in our database.
ReplyDeleteBeautiful. thanks
ReplyDelete