如何检查链接是否可用?
我想创建一个网页,在其中添加一些 Intranet 链接。 我只是想知道如何检查我添加的链接是否有效。 如果链接不起作用,我想将其标记为红色,否则应该为绿色。
I want to create a webpage in which I am adding some intranet links.
I just wanted to know how to check whther the link that i have added, at a moment is working or not.
I want to mark the link in RED if its not working else it should be green.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想要进行轻量级编程检查,可以执行 HTTP HEAD 请求并检查响应代码是否大于或等于 200 且小于 400。
If you want a lightweight programmatic check, you could do an HTTP HEAD request and check for a response code greater than or equal to 200 and less than 400.
如果你想检查客户端...
If you want to check client side...
如果你想检查服务器端(php 示例):
If you want check sever side (php example):
对链接另一端的某些内容执行 Ping 操作。虽然您可能会找到方法来检查接口状态或路由表或各种其他内容...但您真正想知道的是流量是否流入或流出该目的地。
在 unix 上,可以使用
ping
和-c
参数来发送多个数据包,并使用-w
指定等待时间(以秒为单位)。然后检查退出状态:如果数据包丢失,则 ping 将以非零退出代码退出。
Ping something on the other side of the link. While you could probably find ways to check interface status or routing tables or various other things ... what you really want to know is whether the traffic is flowing to and from that destination.
On unix can use
ping
with the-c
argument to send a number of packets, and-w
to specify the wait time in seconds. Then check the exit status:ping will exit with non-zero exit code if packets are dropped.