检查文件是否存在于网络上 [英] Check if file exist exist on the web

查看:91
本文介绍了检查文件是否存在于网络上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何验证网络上是否存在图像或html之类的文件
我已尝试过

how do i verify a file like a image or html exist on the web
i have tried

代码段

推荐答案

您只能验证是否可以访问网络上的文件.您永远不会知道文件是否不存在,因为尝试该服务时该服务可能会脱机,或者速度太慢,或者服务器出于安全原因拒绝响应您.

You can only verify whether you can acess to the file on the web.  You never know whether the file does not exist, because the service could be offline when you try it or it is just too slow, or the server refuses to response to you for security reasons.

您可以使用WebClient类或HttpWebRequest类.您的第一段代码很好,当HttpWebRequest.GetReponse无法连接到服务器或遇到其他网络问题时,它将引发异常.您需要将其放在try-catch块中并处理这些异常,这些异常将告诉您为什么您无法访问该文件.一种可能是文件不存在.

You can use either WebClient class or HttpWebRequest class.  First piece of your code is fine, the HttpWebRequest.GetReponse will throw exception when it fails to connect to the server or hits other network problems.  You need put it inside a try-catch block and handle those exceptions, those exception will tell you why you failed to access that file. One possiblity is that file does not exisit.


这篇关于检查文件是否存在于网络上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆