FTP连接无法正常工作

时间:2014-03-30 15:00:07

标签: c# .net ftp

我尝试连接到ftp服务器并将文件下载到本地文件夹,但我在此行FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();

中遇到异常
  

异常类型:类型的未处理异常   ' System.Net.WebException'发生在System.dll附加   信息:无法解析远程名称:   ' lamerwebmaster.dsl.gekml'

这是我的代码

public static void Main()
{
    string localPath = @"D:\Test";
    List<string> dirListing = new List<string>();
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://address");
    request.Credentials = new NetworkCredential("user", "pass");
    request.Method = WebRequestMethods.Ftp.ListDirectory;

    StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());

    string fileName3 = streamReader.ReadLine();

    while (fileName3 != null)
    {
        dirListing.Add(fileName3);
        fileName3 = streamReader.ReadLine();
    }

    request.Abort();
    streamReader = null;

    for (int i = 0; i < dirListing.Count; i++)
    {            
        if (dirListing[i].Contains(folderName))
        {
            FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create("ftp://address" + dirListing[i]);
            requestFileDownload.Credentials = new NetworkCredential("user", "pass");
            requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;

            FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();

            Stream responseStream = responseFileDownload.GetResponseStream();
            FileStream writeStream = new FileStream(localPath + dirListing[i], FileMode.Create);

            int Length = 2048;
            Byte[] buffer = new Byte[Length];
            int bytesRead = responseStream.Read(buffer, 0, Length);

            while (bytesRead > 0)
            {
                writeStream.Write(buffer, 0, bytesRead);
                bytesRead = responseStream.Read(buffer, 0, Length);
            }

            responseStream.Close();
            writeStream.Close();

            requestFileDownload = null;
            responseFileDownload = null;                        
        }

    }

    Console.ReadKey();
}

0 个答案:

没有答案