Problem sending file via FTP C # [closed]

1

I have an executable that sends a .CSV file via FTP to my client.

Running on my local machine works perfectly but when running on my client's server it does not work.

Whenever it falls on the line

request.GetRequestStream()

I get the error message

  

Unable to connect to the remote server

Follow the code for my application

string pathArquivoConsumoFull = string.Format("{0}\{1}", arquivoData[0].DirectoryName, arquivoData[0].Name);

Console.WriteLine("patharquivoConsumo - " + pathArquivoConsumoFull);

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(enderecoFTP + "/" + Path.GetFileName(arquivoData[0].Name));
request.Method = WebRequestMethods.Ftp.UploadFile;

request.Credentials = new NetworkCredential(nomeUsuarioFTP, passFTP);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;

var stream = File.OpenRead(pathArquivoConsumoFull);
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();

Console.WriteLine("Entra no reqStream");

using (var reqStream = request.GetRequestStream())
{
    reqStream.Write(buffer, 0, buffer.Length);
    reqStream.Close();
}

Console.WriteLine("Passou reqStream!!!");

I installed FileZila on the server and I have access to the FTP address for upload and download .     

asked by anonymous 04.05.2017 / 15:59

2 answers

1

I use the following method for UPLOAD and runs on the client server without problems, make a comparison or even a test according to your needs and see the result.

    public static Byte[] StartUploadsFtp(out string pstrMsg, out bool pbooRetorno, string pstrDiretorioArq, int pnuEndRemotoFtp)
    {
        pstrMsg = default(String);
        pbooRetorno = default(Boolean);
        byte[] buffer = default(Byte[]);

        try
        {
            DataTable dt = Dal.SelectInfoConfigFtpDAL(out pstrMsg, out pbooRetorno, pnuEndRemotoFtp);

            if (dt.Rows.Count > 0 && dt.Rows.Count == 1)
            {
                FileInfo fileInfo = new FileInfo(pstrDiretorioArq);

                var strUsuario = dt.Rows[0]["ftp_usuario"].ToString();
                var strSenha = dt.Rows[0]["ftp_senha"].ToString();
                var strServidor = dt.Rows[0]["ftp_servidor"].ToString();
                var strDiretorioFtp = dt.Rows[0]["diretorio_ftp"].ToString();

                using (FileStream fileStream = File.OpenRead(pstrDiretorioArq))
                {
                    buffer = new byte[fileStream.Length];

                    fileStream.Read(buffer, 0, buffer.Length);

                    // Cria o XML do arquivo Txt, para fazer o Upload para o FTP
                    Uri uri = new Uri(string.Format(@"{0}//{1}//{2}", strServidor, strDiretorioFtp, fileInfo.Name));

                    // Criando uma requisição FTP
                    FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(uri);
                    request.Credentials = new NetworkCredential(strUsuario, strSenha);
                    request.KeepAlive = false;
                    request.Method = WebRequestMethods.Ftp.UploadFile;
                    request.UseBinary = true;
                    request.ContentLength = buffer.Length;

                    // Escreve no arquivo
                    using (Stream stream = request.GetRequestStream())
                    {
                        stream.Write(buffer, 0, buffer.Length);

                        pbooRetorno = true;
                    }
                }
            }
            else
            {
                pstrMsg = string.Format("Não foram encontrados os dados de configuração do FTP.");

                pbooRetorno = false;
            }
        }
        catch (Exception ex)
        {
            pstrMsg = string.Format("Erro:\nMétodo 'StartUploads'\nDetalhes: {0}", ex.Message);

            pbooRetorno = false;
        }
        return buffer;
    }
    
04.05.2017 / 17:50
-1

The problem was the user running my process on Job, he did not have the credentials needed to open a connection to the client's FTP address

    
04.05.2017 / 21:32