Problem sending file via FTP C#

Asked

Viewed 642 times

1

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

Running on my local machine works smoothly but when runs on my client’s server does not work.

Every time I fall in line

request.GetRequestStream()

get the error message

Unable to connect to the remote server

Follow the code of 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 have access to FTP address for upload and download.

  • 1

    There is no way to help you without knowing the details of the machine. Check firewall, proxy these things. If the firewall have locking the doors between 60000 and 61000 that’s the problem.

  • Excuse my ignorance, if the potas between 60000 and 61000 were blocked, Filezila would not be able to connect?

  • Depends on the rules of firewall. May be locked and have a rule releasing all the pro ports Filezilla.

  • I created new rules for the port range. I checked and the Firewall state of the profile is Off. Continues with the same error. some other hint?

  • Specific no. Try opening FTP by browser also, check if there is no restriction with your application.

  • tbm I have access through the browser.

  • What shows the stacktrace?

  • Once it enters the Getrequeststream() line I have the Exception "Unable to connect to the remote server " I tried to get the Innerexception log but it comes null

  • And the stacktrace?

  • Ql the ftp address value in both cases?

  • @jbueno comes null

  • Includes a line and now stacktrace returns at System.Net.Ftpwebrequest.Chekerror()

Show 7 more comments

2 answers

1

I use the following method for UPLOAD and run 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;
    }
  • What value do you pass on (Ftpwebrequest)Ftpwebrequest.Create(??);

  • I pass mine as ftp://54.207.102.23:21/pub/lerCollect/nameFile.csv but I’m sure that’s not the problem

  • I pass this the Uri object that is created just above. It gets something like: ftp: //client.dd.text.com://director_Ftp//filename

  • Maybe it’s the permissions of my exe, when I run it by clicking 2x it works. when I call it from and a procedure in the bank, it doesn’t work

-1

The problem was the user running my process in Job, he did not have the credentials necessary to open a connection to the client’s FTP address

  • 1

    Works in my machine is a classic case of neglecting rights requirements and fail validation/verification rules.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.