您的位置:首页 > 理论基础 > 计算机网络

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

2008-05-16 14:28 399 查看
因为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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: