【问题标题】:FTP downloading a file from a serverFTP 从服务器下载文件
【发布时间】:2015-06-05 13:42:38
【问题描述】:

我正在尝试通过 FTP 从 MOXA UC8410 下载文件。我的代码不起作用。这是我的代码:

 void download2()
        {

            Uri serverUri = new Uri("ftp://169.254.1.1/CFDisk/PCPACM/pcpacm.ini");
            // The serverUri parameter should start with the ftp:// scheme.
            if (serverUri.Scheme != Uri.UriSchemeFtp)
            {

            }
            // Get the object used to communicate with the server.
            WebClient request = new WebClient();

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("admin", "admin");
            try
            {
                byte[] newFileData = request.DownloadData(serverUri.ToString());
                string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
                //Console.WriteLine(fileString);
            }
            catch (WebException e)
            {
                // Console.WriteLine(e.ToString());
                MessageBox.Show(e.Response + e.Message);
            }
        }

我也试过这个:

void download()
        {
            try
            {
                // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://169.254.1.1:21/CFDisk/PCPACM/pcpacm.ini"));
                // using admin as the username and admin as the passward.
                request.Credentials = new NetworkCredential("admin", "admin");
                //request.KeepAlive = false;
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);
                processingfile(reader);
                responseStream.Close();
                reader.Close();
            }
            catch (Exception e2)
            {
                MessageBox.Show("Not connected" , e2.Message);
            }
        }

代码到达

Stream responseStream = response.GetResponseStream();

它只是停止,它永远不会进入下一行。

输出说:

线程 0x175c 已退出,代码为 259 (0x103)。
线程 0x212c 已退出,代码为 259 (0x103)。

这没有帮助。

我可以使用命令提示符 ftp 到 MOXA UC8410,我可以使用 FileZilla 下载文件,但不使用我的代码。 Moxa UC8410 上没有防火墙,所以我的代码很可能有问题。

更新日期: 更新它正在工作!!!

但只有当我转到本地连接属性并将 Internet 协议版本 4(tcp/IPv4) 更改为

使用以下 IP 地址:

IP地址:169.254.1.5

子网掩码:225.225.0.0

有人知道为什么吗?有没有办法在我不必这样做的地方修复它?

为什么我必须把它们放在同一个子域上?

【问题讨论】:

    标签: c# ftp


    【解决方案1】:

    由于我还不能发表评论,所以我将把我的问题放在这里:

    1) 当您使用FileZilla 登录时,您是从CFDisk 所在的目录开始的吗? 2) 您使用的是纯文本 FTP 吗?在 TLS 上使用 FTP 时,方法略有不同。 3)它会抛出异常吗?如果是这样,也请向我们提供更多详细信息。

    另外,WebResponseStreamStreamReader 是一次性的。

    【讨论】:

    • 1) domi1819 使用 FileZila,我没有从 CFDisk 所在的目录开始,我从 169.254.1.1 开始,然后登录/连接,然后移动到文件所在的位置。 2)我不确定你的意思,但拉了一个 .ini 文件。 3)不,它没有,它只是停止工作。一段时间后它会超时。
    • 我想你并没有真正明白我的问题。在您的 FTP 服务器上,登录后,您会登陆哪个文件夹? CFDisk 是否在 admin 的主目录中?
    • 它说远程站点:/ 所以我在 / 目录中。但我不在 CFDisk 中。我可以从/目录去那个
    【解决方案2】:

    我创建了(并在我的一台远程服务器上进行了测试)这个小的 Windows 窗体应用程序。它似乎在这里工作正常。看看吧。

    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    
    namespace FTPTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string localPath = @"C:\Temp\navigazione-privata.pdf";
                string ftpIPAddress = "xxx.xxx.xxx.xxx";
                string remoteFilepath = "/userdownloads/navigazione-privata.pdf";
    
                string ftpPath = "ftp://" + ftpIPAddress + remoteFilepath;
    
                using (WebClient request = new WebClient())
                {
                    request.Credentials = new NetworkCredential("username", "password");
                    byte[] fileData = request.DownloadData(ftpPath);
    
                    using (FileStream file = File.Create(localPath))
                    {
                        file.Write(fileData, 0, fileData.Length);
                        file.Close();
                    }
                    MessageBox.Show("Requested file downloaded");
                }
            }
        }
    }
    

    【讨论】:

    • 我刚刚尝试了该代码,但它不起作用。这就是我得到的线程 0x2160 已退出,代码为 259 (0x103)。线程 0x924 已退出,代码为 259 (0x103)。
    • hmmmmm... 退出代码 259 是“STILL_ACTIVE”,这是来自 VS 的调试消息。你在使用 VS 2013 吗?阅读this article。您也可以尝试使用Wireshark 来了解运行函数时网络中发生的情况。正如我所说,我之前在这里发布的功能就像一个魅力......
    • 无论如何......我认为您看到的退出代码与您的代码无关。它们可能只是 Visual Studio 调试消息,或终止的 ThreadPool 线程。请再次检查 FTP 和本地文件夹参数以确保它们正确。如果您还没有,请尝试使用 FTP 客户端执行相同的操作(例如 Filezilla)。