连接FTP服务器的凭据 [英] Connecting ftp server with credentials

查看:400
本文介绍了连接FTP服务器的凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写使用带有凭据的FTP服务器程序。我试图从服务器检索目录列表中,但是当我站上罚球线:

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();

这是我得到的字符串只包含:无法打开\主持人:/ lib1\\ 。\\

the string that I get contains only : "Can't open \"host:/lib1\"."

如果我试图让另一条线,下抛出异常:远程服务器返回错误:(550)文件不可用(例如,文件不发现,没有访问权限)。

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).

我知道肯定(使用其他FTP应用程序)存在的FTP服务器上LIB1目录和我的凭据(用户名和密码)是。正确的

I know for sure (using another ftp application) that 'lib1' directory exists on the ftp server and my credentials (username and password) are correct.

下面是我的代码:

 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?

推荐答案

我的解决办法:

public string[] GetDirectory()
{
    StringBuilder result = new StringBuilder();
    FtpWebRequest requestDir = (FtpWebRequest)WebRequest.Create("ftp://urserverip/");
    requestDir.Method = WebRequestMethods.Ftp.ListDirectory;
    requestDir.Credentials = new NetworkCredential("username", "password");
    FtpWebResponse responseDir = (FtpWebResponse)requestDir.GetResponse();
    StreamReader readerDir  = new StreamReader(responseDir.GetResponseStream());

    string line = readerDir.ReadLine();
    while (line != null)
    {
        result.Append(line);
        result.Append("\n");
        line = readerDir.ReadLine();
    }

    result.Remove(result.ToString().LastIndexOf('\n'), 1);
    responseDir.Close(); 
    return result.ToString().Split('\n');
}

这篇关于连接FTP服务器的凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆