zoukankan      html  css  js  c++  java
  • c# 判断远程文件是否存在

    代码
    //1:
    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, falsenullnull);
      _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;
      }
    }
  • 相关阅读:
    Dropdown 下拉菜单
    Breadcrumb 面包屑
    Tabs 标签页
    NavMenu 导航菜单
    Notification 通知
    MessageBox 弹框
    Message 消息提示
    Loading 加载
    Alert 警告
    Badge 标记
  • 原文地址:https://www.cnblogs.com/hantianwei/p/1717681.html
Copyright © 2011-2022 走看看