ASP+XMLHTTP检测文件是否存在(掉链)

因为http://www.164.cc/blog的音乐文件来自互联网搜索,各种原因使得音乐文件经常会掉链或者就成了死链,要解决这个问题必须先知道哪些歌曲链接掉了。

检测远端文件是否存在,使用XMLHTTP与远端文件交互返回的Status而做出决定

有关Status,参看 http://www.cnbruce.com/blog/showlog.asp?cat_id=34&log_id=1140

已经完成功能的函数如下,此前请确保本地已经安装有 msxml3.dll

(在电脑上注册这个文件,请点击“开始>运行”然后输入:regsvr32 msxml3.dll


Function chku(murl)
on error resume next
Set xmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
xmlHttp.open "GET",murl,false
xmlHttp.send
    if xmlHttp.Status <> 200 then
        chku=false
    else
        chku=true
    end if
End Function


自定义的函数chku(),通过XMLHTTP对象于相关文件(路径)交互返回状态值Status,通过该Status值做出判断函数输出。

完整的检测代码如下:


<%
url=request.querystring("u")

Function chku(murl)
on error resume next
Set xmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
xmlHttp.open "GET",murl,false
xmlHttp.send
    if xmlHttp.Status <> 200 then
        chku=false
    else
        chku=true
    end if
End Function

if chku(url) then
    response.write(url&"(链接正常)")
else
    response.write(url&"(掉链、坏链、死链)")
end if
%>


测试网页时带参数u,参数u的值即是远程文件(路径)比如:

http://www.cnbruce.com/test/ajax/chku.asp?u=http://www.164.cc

http://www.cnbruce.com/test/ajax/chku.asp?u=http://www.dw8.cn/nihaohello
posted @ 2008-05-16 14:28  Athrun  阅读(512)  评论(0编辑  收藏  举报