0

I am trying to download a file from a MOXA UC8410 over FTP. My code is not working. Here is my code:

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

I have also tried this :

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

The code get to

Stream responseStream = response.GetResponseStream();

and it just stops, it never goes to the next line.

output says:

The thread 0x175c has exited with code 259 (0x103).
The thread 0x212c has exited with code 259 (0x103).

which does not help.

I can ftp to the MOXA UC8410 using the command prompt and I can download the file using FileZilla but not using my code. There is no firewall on the Moxa UC8410, so something most be wrong with my code.

UpDate: UpDATE It is working !!!

but only if I go to local Area Connection Properties and change Internet Protocol Version 4(tcp/IPv4) to

use the following IP address:

IP address: 169.254.1.5

Subnet mask: 225.225.0.0

does anyone know why? and is there a way I can fix it where I do not have to do that ?

Why do I have to put them on the same sub domain ?

Brandon
  • 187
  • 1
  • 3
  • 11

2 Answers2

0

Since I cannot comment yet, I'll put my questions here:

1) When you log in using FileZilla, are you starting in the directory where CFDisk lies? 2) Are you using plain-text FTP? When using FTP over TLS, the approach is slightly different. 3) Does it throw an Exception? If so, please also give us more details about that.

Also, WebResponse, Stream and StreamReader are disposable.

Domi
  • 272
  • 1
  • 3
  • 9
  • 1) domi1819 using FileZila, I am not starting in the directory where CFDisk lies, I start in 169.254.1.1 and than I login/connect and then I move to where the file is. 2) I am not sure what you mean but pulling a .ini file over. 3) NO, it does not , it just stops working. after a while it will throw a time out . – Brandon Jun 05 '15 at 14:13
  • I think you didn't really get my question. On your FTP server, after you have logged in, in which folder do you land? Is _CFDisk_ in _admin_'s home directory? – Domi Jun 05 '15 at 14:20
  • it says Remote site : / so i am in the / directory. but I am not in the CFDisk. I can go to that from the / directory – Brandon Jun 05 '15 at 14:34
0

I created (and tested on one of my remote servers) this little Windows Form application. It seems to work fine here. Give it a look.

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");
            }
        }
    }
}
radiolondra
  • 293
  • 4
  • 15
  • I just tried that code but it did not work. This is what I got The thread 0x2160 has exited with code 259 (0x103). The thread 0x924 has exited with code 259 (0x103). – Brandon Jun 05 '15 at 15:08
  • hmmmmm... Exit Code 259 is "STILL_ACTIVE", this is a debug message from VS. Are u using VS 2013? Read [this article](http://stackoverflow.com/questions/22395396/why-am-i-seeing-multiple-the-thread-0x22c8-has-exited-with-code-259-0x103-m) about. Also you could try to use [Wireshark](http://www.howtogeek.com/104278/how-to-use-wireshark-to-capture-filter-and-inspect-packets/) to understand what is happening in your network while running your function. As I said, the function I posted before here is running like a charme... – radiolondra Jun 05 '15 at 16:09
  • and anyway.... I think that the exit codes you see have nothing to do with your code. they might simply be Visual Studio debug messages, or terminated ThreadPool threads. Please check again the FTP and local folder parameters to be sure they are correct. If you didn't yet, try with a FTP client to do the same (like Filezilla). – radiolondra Jun 05 '15 at 16:28