0

I'm working on a project, that collects some informations to a .CSV file and then send the file to a ftp server.

The program sends the file successfully sometimes, but other times it just stops and throws the error:

An unhandled exception of type 'System.Net.WebException' occurred in System.dll

Additional information: The remote server returned an error: 227 Entering Passive Mode (192,168,10,170,216,244)

Can anyone spot anything, that I have done wrong?

Thanks in advance.

Here is my code

        private void sendFile_Click(object sender, EventArgs e)
    {
        Upload("ftp://100.64.44.12", "UsernameHere", "PasswordHere", @"C:\Users\Kasper\Documents\testFolder\data.csv");
    }

    public void Upload(string FTPAddress, string username, string password, string filePath)
    {
        FileStream stream = File.OpenRead(filePath);
        byte[] buffer = new byte[stream.Length];
        stream.Read(buffer, 0, buffer.Length);
        stream.Close();

        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" +
        Path.GetFileName(filePath));
        request.Method = WebRequestMethods.Ftp.UploadFile;

        request.Credentials = new NetworkCredential(username, password);
        request.UsePassive = true;
        request.UseBinary = false;
        request.KeepAlive = true;

        StreamReader sourceStream = new StreamReader("testfile.txt");
        byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        MessageBox.Show("Upload File Complete, status {0}", response.StatusDescription);

        response.Close();
    }
Kasper Hansen
  • 72
  • 1
  • 10
  • Leave the Passive mode on. 500 server error occurs when the server only allows passive connections. Get the trace of system.net you'll find something there about why the error occurred. Usually it's firewall. – vendettamit Mar 22 '16 at 16:33
  • [Enable logging](http://stackoverflow.com/q/9664650/850848) and show us a log file both for the successful upload and the failed one. – Martin Prikryl Mar 23 '16 at 06:51

1 Answers1

-1

Try setting FtpWebRequest.UsePassive = false

Pedro G. Dias
  • 3,162
  • 1
  • 18
  • 30
  • Stream requestStream = request.GetRequestStream(); – Kasper Hansen Mar 22 '16 at 15:56
  • this line gets this error An unhandled exception of type 'System.Net.WebException' occurred in System.dll Additional information: The remote server returned an error: (500) Syntax error, command unrecognized. – Kasper Hansen Mar 22 '16 at 15:57
  • @KasperHansen Turn on the tracing for `System.net` [instructions here](https://blogs.msdn.microsoft.com/dgorti/2005/09/18/using-system-net-tracing/) and post the trace info when this exception occurs. – vendettamit Mar 22 '16 at 16:28
  • What you don't understand? Have you visited the link I referred? Checkout this thread https://social.msdn.microsoft.com/Forums/en-US/47634ec2-4d40-4d3f-b075-8cc92bfa2b24/what-is-keep-alive-property-for-in-ftpwebrequest?forum=ncl to understand how the trace info can help you detecting the issue. – vendettamit Mar 22 '16 at 16:49