以下测试工作...
public void test1()
{
string server="ftp://myserver.com/dev";
string userName="myusername";
string password="mypassword";
FtpWebRequest req = (FtpWebRequest)WebRequest.Create( server );
req.Credentials = new NetworkCredential( userName, password );
req.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
req.Timeout = 30000;
req.UseBinary = false;
req.EnableSsl = false;
req.UsePassive = false;
req.KeepAlive = true;
using( FtpWebResponse resp = (FtpWebResponse)req.GetResponse() )
{
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
string fileRecord = sr.ReadLine();
while (fileRecord != null)
{
Console.WriteLine( fileRecord );
fileRecord = sr.ReadLine();
}
}
}
}
虽然以下测试失败...
public void test2()
{
string server="ftp://myserver.com/dev";
string userName="myusername";
string password="mypassword";
FtpWebRequest req = (FtpWebRequest)WebRequest.Create( server );
req.Credentials = new NetworkCredential( userName, password );
req.Method = WebRequestMethods.Ftp.GetDateTimestamp;
req.Timeout = 30000;
req.UseBinary = false;
req.EnableSsl = false;
req.UsePassive = false;
req.KeepAlive = true;
using( FtpWebResponse resp = (FtpWebResponse)req.GetResponse() )
{
using( StreamReader sr = new StreamReader( resp.GetResponseStream() ) )
{
Console.WriteLine( resp.LastModified );
}
}
}
带有错误消息:
测试方法test2抛出异常:System.Net.WebException:远程服务器返回错误:(550)文件不可用(例如,找不到文件,无法访问)。
更新:我尝试使用另一个使用默认端口号的 ftp 站点(unix),所以 url 是“ ftp://myserver.com/dev ” - 并且 GetDateTimestamp() 仍然因相同的错误而死。
我已更新主题行和问题正文以正确反映我的查询。