为什么FtpWebRequest返回此现有目录的空流? [英] Why does FtpWebRequest return an empty stream for this existing directory?
问题描述
我不确定为什么会得到这个结果.我正在Linux服务器上运行它. (这是我的小型网站的共享虚拟主机帐户.)
I'm not sure why am I getting this result. I'm running this on a Linux server. (It's my small web site's shared web hosting account.)
文件分组如下:
和Another Dir
内部有一个文件:
因此,我尝试使用以下代码在服务器上不存在的1somelongdir1234567
目录中检索badname
目录的内容:
So I'm trying to retrieve the contents of the badname
directory inside 1somelongdir1234567
directory that doesn't exist on the server, using this code:
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
,就像我在上面的代码中显示的那样.
So as you see, there's no such folder badname
but instead of throwing an exception, my ftpRequest.GetResponse()
succeeds and then streamReader.ReadLine()
returns null
like I showed in the code above.
更奇怪的是,如果我提供这样的实际目录:
More strangely, if I provide an actual directory as such:
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(
"ftp://server12.some-domain.com/public_html/1somelongdir1234567/Another%20Dir");
streamReader.ReadLine()
仍返回null
.
有人可以解释为什么吗?
Can someone explain why?
好的,伙计们,我更新了上面的代码以检索状态码.不过我还是很困惑.
OK, guys, I updated the code above to retrieve the status code. I'm still puzzled though.
首先,这是连接URI的三个值以及我得到的响应/输出:
First, here's three values of connection URI and the response/output that I'm getting:
示例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的结果相同?
So why is it giving me the same result for example 2 as I'm getting for 3?
关于它是什么ftp服务器,我无法在网络日志或FtpWebRequest
本身中看到它.这就是我从Microsoft Network Monitor
那里得到的东西:
As for what ftp server it is, I wasn't able to see it in the network logs nor in FtpWebRequest
itself. Here's what I was able to get from Microsoft Network Monitor
:
欢迎使用Pure-FTPd [privsep] [TLS] ---------- ..
220-您是允许的50用户编号4 ... 220-当地时间现在是13:14.服务器端口:21 ...
220-这是一个私人系统-没有匿名登录. 此服务器上也欢迎220-IPv6连接...
220您将在闲置15分钟后断开连接...
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
/ListDirectoryDetails
方法的URL通常应以斜杠结尾.
The URL for the ListDirectory
/ListDirectoryDetails
method should end with a slash, in general.
不加任何斜杠,结果往往是不确定的.
Without a slash, results tend to be uncertain.
WebRequest.Create("ftp://example.com/public_html/1somelongdir1234567/Another%20Dir/");
这篇关于为什么FtpWebRequest返回此现有目录的空流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!