我不确定为什么会得到这个结果。我在 Linux 服务器上运行它。 (这是我的小型网站的共享网络托管帐户。)
文件分组如下:
and the Another Dir
里面有一个文件:
所以我试图检索的内容badname
里面的目录1somelongdir1234567
服务器上不存在的目录,使用以下代码:
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");
the streamReader.ReadLine()
仍然返回null
.
有人可以解释为什么吗?
编辑:好的,伙计们,我更新了上面的代码来检索状态代码。但我还是很困惑。
首先,这是连接 URI 的三个值以及我得到的响应/输出:
示例1:
//Existing folder
"ftp://server12.some-domain.com/public_html/1somelongdir1234567"
Output:
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"
Output:
150 Accepted data connection
OpeningData
示例3:
//Nonexistent folder
"ftp://server12.some-domain.com/public_html/1somelongdir1234567/SomeBogusName"
Output:
150 Accepted data connection
OpeningData
那么为什么示例 2 给出的结果与示例 3 得到的结果相同呢?
至于它是什么 ftp 服务器,我无法在网络日志中或在FtpWebRequest
本身。这是我能从中得到的Microsoft Network Monitor
:
欢迎来到 Pure-FTPd [privsep] [TLS] ----------..
220-您是允许的 50 个用户中的第 4 个用户...
220-当地时间现在是 13:14。服务器端口:21...
220-这是一个私人系统 - 无法匿名登录..
该服务器也欢迎 220-IPv6 连接...
220 15 分钟不活动后您将断开连接...