zoukankan      html  css  js  c++  java
  • ftp访问空目录的返回

    protected override bool DirectoryExists(string path)
            {
                bool exists = false;
                if (path.LastIndexOf('/') != path.Length - 1)
                {
                    path = path + '/';
                }
                FtpWebResponse response = null;
                FtpWebRequest request = null;
                int retryCount = 0;
                while (true)
                {
                    try
                    {
                        request = CreateFtpRequest("ftp://" + HostIP + path);
                        request.Method = WebRequestMethods.Ftp.ListDirectory;
                        response = (FtpWebResponse)request.GetResponse();
                        using (TextReader tr = new StreamReader(response.GetResponseStream()))
                        {
                            string text = tr.ReadToEnd();
                            exists = true;
                        }
                        break;
                    }
                    catch (WebException e)
                    {
                        if (response != null && response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                        {
                            exists = false;
                            break;
                        }
                        else
                        {
                            response = e.Response as FtpWebResponse;
                            if (response != null && response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                            {
                                exists = false;
                                break;
                            }
                        }
                        
                        if (HandleWebException(e, ++retryCount))
                        {
                            continue;
                        }
                    }
                    finally
                    {
                        if (response != null)
                        {
                            response.Close();
                            response = null;
                        }
                    }
                }
    
                return exists;
            }

    这个方法是用来判断ftp路径是否存在,一开始并没有去判断path变量末尾有没有"/",后来发现,如果末尾没有"/",即使访问了一个空目录,也不会抛出异常,也就是说会返回一个550,但是不会抛出异常。

  • 相关阅读:
    structInMemory
    合并字符串
    eggs
    1005. Spell It Right (20) -PAT
    60 人工智能
    50 计算机网络
    20数据结构
    40操作系统
    10 C/C++/python
    30汇编
  • 原文地址:https://www.cnblogs.com/middlesummer/p/3555306.html
Copyright © 2011-2022 走看看