使用凭据连接 ftp 服务器
我正在编写一个使用带有凭据的 ftp 服务器的程序。我正在尝试从服务器检索目录列表,但是当我到达该行时:
string line = reader.ReadLine();
我得到的字符串仅包含:“无法打开\”主机:/ lib1 \“。”
如果我尝试获取另一行,则会引发下一个异常:远程服务器返回错误:(550) 文件不可用(例如,找不到文件,无法访问)。
我确信(使用另一个 ftp 应用程序)ftp 服务器上存在“lib1”目录,并且我的凭据(用户名和密码)是正确的。
这是我的代码:
public class FTPClient
{
public string UserName { get; set; }
public string Password { get; set; }
public string IpAddress { get; set; }
public int Port { get; set; }
public FTPClient(string _userName, string _password, string _address, int _port)
{
UserName = _userName;
Password = _password;
IpAddress = _address;
Port = _port;
}
public void GetDirectoriesList(string _path)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" +
IpAddress + _path));
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(UserName, Password);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string line = reader.ReadLine();
while (line!=null)
{
... //do something with line
line = reader.ReadLine();
}
...
reader.Close();
response.Close();
}
我按如下方式使用它:
FTPClient ftpClient = new FTPClient("user1", "pass1", "192.168.2.110", 21);
string dirList = ftpClient.GetDirectoriesList("/lib1");
有人能发现问题吗?
I'm writing a program that uses an ftp server with credentials. I'm trying to retrieve the directory list from the server but when I get to the line:
string line = reader.ReadLine();
the string that I get contains only : "Can't open \"host:/lib1\"."
If I try to get another line, the next exception is thrown: The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
I know for sure (using another ftp application) that 'lib1' directory exists on the ftp server and my credentials (username and password) are correct.
Here is my code:
public class FTPClient
{
public string UserName { get; set; }
public string Password { get; set; }
public string IpAddress { get; set; }
public int Port { get; set; }
public FTPClient(string _userName, string _password, string _address, int _port)
{
UserName = _userName;
Password = _password;
IpAddress = _address;
Port = _port;
}
public void GetDirectoriesList(string _path)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" +
IpAddress + _path));
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(UserName, Password);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string line = reader.ReadLine();
while (line!=null)
{
... //do something with line
line = reader.ReadLine();
}
...
reader.Close();
response.Close();
}
And I use it as follows:
FTPClient ftpClient = new FTPClient("user1", "pass1", "192.168.2.110", 21);
string dirList = ftpClient.GetDirectoriesList("/lib1");
Can anyone spot the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的解决方案:
My solution:
对 Abdul Waheed 的答案进行了一些改进:
using
块来清理FtpWebResponse< /code> 和
StreamReader
对象;减少字符串操作:
Some refinements to Abdul Waheed's answer:
using
blocks to clean up theFtpWebResponse
andStreamReader
objects;Reduced string manipulation: