c# 判断远程文件是否存在
发布时间:2010年06月22日点击数:
次佚名
- public static bool IsExist(string uri)
- {
- HttpWebRequest req = null;
- HttpWebResponse res = null;
- try
- {
- req = (HttpWebRequest)WebRequest.Create(uri);
- req.Method = "HEAD";
- req.Timeout = 100;
- res = (HttpWebResponse)req.GetResponse();
- return (res.StatusCode == HttpStatusCode.OK);
- }
- catch
- {
- return false;
- }
- finally
- {
- if (res != null)
- {
- res.Close();
- res = null;
- }
- if (req != null)
- {
- req.Abort();
- req = null;
- }
- }
- }
- private bool UrlExistsUsingXmlHttp(string url)
- {
-
- MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
- _xmlhttp.open("HEAD", url, false, null, null);
- _xmlhttp.send("");
- return (_xmlhttp.status == 200);
- }
-
-
- private bool UrlExistsUsingSockets(string url)
- {
- if (url.StartsWith("http://")) url = url.Remove(0, "http://".Length);
- try
- {
- System.Net.IPHostEntry ipHost =System.Net.Dns.GetHostEntry(url);
- return true;
- }
- catch (System.Net.Sockets.SocketException se)
- {
- System.Diagnostics.Trace.Write(se.Message);
- return false;
- }
- }