用c#判断远程文件是否存在

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;
                    }
                }
            }

    //2:

    private bool UrlExistsUsingXmlHttp(string url)
    {
      //注意:此方法需要引用Msxml2.dll
      MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
      _xmlhttp.open("HEAD", url, false, null, null);
      _xmlhttp.send("");
      return (_xmlhttp.status == 200);
    }

    //3:
    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);// System.Net.Dns.Resolve(url);
        return true;
      }
      catch (System.Net.Sockets.SocketException se)
      {
        System.Diagnostics.Trace.Write(se.Message);
        return false;
      }
    }

时间: 2024-08-01 02:00:07

用c#判断远程文件是否存在的相关文章

判断远程文件是否存在的php函数

函数|是否存在 判断远程的文件是否存在. <?php /*  函数:remote_file_exists  功能:判断远程文件是否存在  参数: $url_file - 远程文件URL  返回:存在返回true,不存在或者其他原因返回false*/function remote_file_exists($url_file){ //检测输入 $url_file = trim($url_file); if (empty($url_file)) { return false; } $url_arr =

PHP利用curl判断远程文件是否存在的方法

  PHP利用curl判断远程文件是否存在,请看下边的代码: //判断远程文件 function check_remote_file_exists($url) { $curl = curl_init($url); // 不取回数据 curl_setopt($curl, CURLOPT_NOBODY, true); // 发送请求 $result = curl_exec($curl); $found = false; // 如果请求没有发送失败 if ($result !== false) { /

C# 判断远程文件是否存在

#region 判断远程文件是否存在 /// <summary> /// 判断远程文件是否存在 /// </summary> /// <param name="fileUrl"></param> /// <returns></returns> public static bool RemoteFileExists(string fileUrl) { HttpWebRequest re = null; HttpWeb

PHP使用get_headers函数判断远程文件是否存在的方法_php技巧

本文实例讲述了PHP使用get_headers函数判断远程文件是否存在的方法.分享给大家供大家参考.具体实现方法如下: 以前讲过程关于php判断远程文件是否存在的文章都是利用fopen,sockt,curl函数来实现检查远程文件是否存在,下面我再介绍利用 get_headers来检查远程文件是否存在,感兴趣的朋友可以参考一下. 先来简单了解get_headers()函数 get_headers() 返回一个数组m包含有服务器响应一个 HTTP 请求所发送的标头. get_headers:发送服务

php下利用curl判断远程文件是否存在的实现代码_php技巧

复制代码 代码如下: //判断远程文件 function check_remote_file_exists($url) { $curl = curl_init($url); // 不取回数据 curl_setopt($curl, CURLOPT_NOBODY, true); // 发送请求 $result = curl_exec($curl); $found = false; // 如果请求没有发送失败 if ($result !== false) { // 再检查http响应码是否为200 $

PHP get_headers函数判断远程文件是否存在

先来简单了解get_headers()函数 get_headers() 返回一个数组,包含有服务器响应一个 HTTP 请求所发送的标头. get_headers:发送服务器响应HTTP请求 get_headers(字符串url[链接格式]) get_headers()以数组的形式返回服务器HTTP请求.如果执行失败,将返回FALSE和一个错误的水平E_WARNING>. 可选参数设置为1,get_headers()能分析系统的响应速度和集数组中的键. 注意:使用该函数需要把 php.ini里面的

php提前判断远程文件是否可用

 代码如下 复制代码 //判断远程文件 function check_remote_file_exists($url) { $curl = curl_init($url); // 不取回数据 curl_setopt($curl, CURLOPT_NOBODY, true); // 发送请求 $result = curl_exec($curl); $found = false; // 如果请求没有发送失败 if ($result !== false) { // 再检查http响应码是否为200 $

[求助]问一个关于远程文件确定的菜鸟问题,怎么判断远程文件和文件夹??

问题描述 我用一个方法得到了FTP上的目录及文件,(用的是WebRequestMethods.Ftp等实现),填充到了一个字符数组中(string[]list)现在,我有一个问题,如果想判断list数组中的每一项是文件还是文件夹呢?请大侠们指点江山. 解决方案 解决方案二:我用过一个办法,就是一般文件都有括展名,所以文件都包含一个字符".",文件夹一般不包含,但是这种方法毕竟不是好的方法,谁还有方法?解决方案三:精神可佳,鼓励一下自己.解决方案四:你得到的时候,能判断文件和文件夹?如果

判断远程文件是否存在

        private bool RemoteFileExists(string fileUrl)        {            try            {                HttpWebRequest re = (HttpWebRequest)WebRequest.Create(fileUrl);                HttpWebResponse res = (HttpWebResponse)re.GetResponse();