我正在编写一个使用带有凭据的 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();
我得到的字符串只包含:无法打开"host:/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 应用程序)'lib1' 目录存在于 ftp 服务器上,并且我的凭据(用户名和密码)是正确的.
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();
}
我使用它如下:
FTPClient ftpClient = new FTPClient("user1", "pass1", "192.168.2.110", 21);
string dirList = ftpClient.GetDirectoriesList("/lib1");
谁能发现问题?
我的解决方案:
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("
");
line = readerDir.ReadLine();
}
result.Remove(result.ToString().LastIndexOf('
'), 1);
responseDir.Close();
return result.ToString().Split('
');
}
这篇关于使用凭据连接 ftp 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
ASP.NET Core 使用 Azure Active Directory 进行身份验证并ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 进行身
ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用)
如何获取守护进程或服务器到 C# ASP.NET Web API 的How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何获取守护进程或服务器到 C# ASP.N
异步调用时 Azure KeyVault Active Directory AcquireTokenAAzure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(异步调用时 Azure KeyVault Active Directory AcquireTokenAsync 超
使用电子邮件地址和应用程序密码从 oauth2/tokenGetting access token using email address and app password from oauth2/token(使用电子邮件地址和应用程序密码从 oauth2/token 获取访问令
新的 Azure AD 应用程序在通过管理门户更新之前无New Azure AD application doesn#39;t work until updated through management portal(新的 Azure AD 应用程序在通过管理门户更新之前无法运行