H
H
Homer_Simpson2014-08-23 23:17:06
FTP
Homer_Simpson, 2014-08-23 23:17:06

Why doesn't uploading a file to an FTP server using C# work?

I'm trying to upload a file to an FTP server using C#, but I get the error "The underlying connection was closed: The server violated the protocol." A Google search brought up the same error, where the following code helped the person, but I still get this error.

The code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;

namespace SteamSSFN
{
    class Program
    {
        static void Main()
        {
            ftpfile("/file.txt", "C:\\file.txt");  
        }

        public static void ftpfile(string ftpfilepath, string inputfilepath)
        {
            string ftphost = "ip:port";
            string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
            ftp.Credentials = new NetworkCredential("user", "pass");
            ftp.KeepAlive = true;
            ftp.UseBinary = true;
            ftp.Proxy = null;
            ftp.Method = WebRequestMethods.Ftp.UploadFile;
            FileStream fs = File.OpenRead(inputfilepath);
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();
            Stream ftpstream = ftp.GetRequestStream();
            ftpstream.Write(buffer, 0, buffer.Length);
            ftpstream.Close();
        }

    }
}


The firewall shows that the request goes through TCP. How can I explicitly set the protocol?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DancingOnWater, 2014-08-24
@DancingOnWater

Brr, you first look with a sniffer what kind of packets are coming.
And yes, it's better to use webclient

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question