Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

Problem with SFTP

Posted on by 45

The code I use is the following:

private void enviarSFTP(Filename fileToSend)
{
    System.Object                   ftpo;
    System.Object                   ftpResponse;
    System.Net.FtpWebRequest        request;
    System.IO.StreamReader          reader;
    System.IO.Stream                requestStream;
    System.Byte[]                   bytes;
    System.Net.NetworkCredential    credential;
    System.Text.Encoding            utf8;
    System.Net.FtpWebResponse       response;
    System.Exception                netExcepn;
    FERR_FTP_PagoATerceros      configuracionFTP;

    int                         fileCount;
    System.Array                files;
    int                         i;
    str                         nextFile;
    Filename                    filepath;
    Filename                    fileType, fileNameString;
    FileNameType                pattern = '*.TXT';
    FileName                    ftpFileName, fileName;

    select firstOnly configuracionFTP where configuracionFTP.BankCode == '1';

    new InteropPermission(InteropKind::ClrInterop).assert();

    try
    {
        if (fileToSend)
        {
                [filepath, filename, fileType] = fileNameSplit(fileToSend);
                fileNameString = fileName + fileType;
                //ftpFileName = 'ftp://' + configuracionFTP.FTPAddress + ':' + configuracionFTP.FTPPort + '/' + configuracionFTP.FTPLocation + '/' + fileNameString;
                ftpFileName = 'ftp://' + configuracionFTP.FTPAddress + '/' + configuracionFTP.FTPLocation + '/' + fileNameString;
                
                reader = new System.IO.StreamReader(fileToSend);
                utf8 = System.Text.Encoding::get_UTF8();
                bytes = utf8.GetBytes(reader.ReadToEnd());
                reader.Close();
                
                ftpo = System.Net.WebRequest::Create(ftpFileName);
                request = ftpo;

                credential = new System.Net.NetworkCredential(configuracionFTP.FTPUser, configuracionFTP.FTPPassword);
                request.set_Credentials(credential);
                request.set_ContentLength(bytes.get_Length());
                request.set_Method("STOR");
                requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.get_Length());
                requestStream.Close();

                ftpResponse = request.GetResponse();
                response = ftpResponse;
                info(response.get_StatusDescription());
        }
    }
    catch(Exception::CLRError)
    {
        netExcepn = CLRInterop::getLastException();
        error(netExcepn.ToString());
    }

    CodeAccessPermission::revertAssert();

Apparently the problem is because the code is for an FTP connection and I need an SFTP connection, indicating the type of protocol to use and the SCP delay.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans