Skip to main content
 首页 » 编程设计

c#之为什么 FtpWebRequest 会为此现有目录返回一个空流

2024年02月27日31jackei

我不确定为什么会得到这个结果。我在 Linux 服务器上运行它。 (这是我的小型网站的共享虚拟主机帐户。)

文件分组如下:

Another Dir 中有一个文件:

因此,我尝试使用以下代码检索服务器上不存在的 1somelongdir1234567 目录中的 badname 目录的内容:

try 
{ 
    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create( 
        "ftp://server12.some-domain.com/public_html/1somelongdir1234567/badname"); 
    ftpRequest.EnableSsl = true; 
    ftpRequest.Credentials = new NetworkCredential("user", "password"); 
    ftpRequest.KeepAlive = true; 
    ftpRequest.Timeout = -1; 
 
    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; 
 
    using (FtpWebResponse response1 = (FtpWebResponse)ftpRequest.GetResponse()) 
    { 
        //*****BEGIN OF EDIT***** 
        Console.WriteLine(response1.StatusDescription); 
        Console.WriteLine(response1.StatusCode); 
        //*****END OF EDIT***** 
 
        using (StreamReader streamReader = new StreamReader(response1.GetResponseStream())) 
        { 
            List<string> arrList = new List<string>(); 
 
            for (; ; ) 
            { 
                string line = streamReader.ReadLine(); 
 
                //I get to here, where `line` is null???? 
 
                if (string.IsNullOrEmpty(line)) 
                    break; 
 
                arrList.Add(line); 
 
                //*****BEGIN OF EDIT***** 
                Console.WriteLine(line); 
                //*****END OF EDIT***** 
            } 
        } 
    } 
} 
catch (Exception ex) 
{ 
    Console.WriteLine(ex.Message); 
} 

如您所见,没有这样的文件夹 badname 但我的 ftpRequest.GetResponse() 没有抛出异常,而是成功了,然后 streamReader.ReadLine( ) 返回 null 就像我在上面的代码中显示的那样。

更奇怪的是,如果我提供一个这样的实际目录:

    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create( 
        "ftp://server12.some-domain.com/public_html/1somelongdir1234567/Another%20Dir"); 

streamReader.ReadLine() 仍然返回 null。

谁能解释一下为什么?


编辑:好的,伙计们,我更新了上面的代码以检索状态代码。不过我还是很困惑。

首先,这是连接 URI 的三个值和我得到的响应/输出:

示例 1:

//Existing folder 
"ftp://server12.some-domain.com/public_html/1somelongdir1234567" 

输出:

150 Accepted data connection 
 
OpeningData 
drwxr-xr-x    3 username   username         4096 Sep  5 05:51 . 
drwxr-x---  118 username   99               4096 Sep  5 05:54 .. 
drwxr-xr-x    2 username   username         4096 Sep  5 05:52 Another Dir 
-rw-r--r--    1 username   username           11 Sep  5 05:51 test123.txt 

示例 2:

//Another existing folder 
"ftp://server12.some-domain.com/public_html/1somelongdir1234567/Another%20Dir" 

输出:

150 Accepted data connection 
 
OpeningData 

示例 3:

//Nonexistent folder 
"ftp://server12.some-domain.com/public_html/1somelongdir1234567/SomeBogusName" 

输出:

150 Accepted data connection 
 
OpeningData 

那么,为什么示例 2 的结果与示例 3 的结果相同?

至于它是什么 ftp 服务器,我无法在网络日志或 FtpWebRequest 本身中看到它。以下是我能够从 Microsoft Network Monitor 获得的内容:

Welcome to Pure-FTPd [privsep] [TLS] ----------..
220-You are user number 4 of 50 allowed... 220-Local time is now 13:14. Server port: 21...
220-This is a private system - No anonymous login..
220-IPv6 connections are also welcome on this server...
220 You will be disconnected after 15 minutes of inactivity...

请您参考如下方法:

ListDirectory/ListDirectoryDe​​tails 方法的 URL 通常应以斜杠结尾。

没有斜线,结果往往是不确定的。

WebRequest.Create("ftp://example.com/public_html/1somelongdir1234567/Another%20Dir/");