Skip to main content

Notifications

Microsoft Dynamics AX (Archived)

Upload Filet to FTP return Error

Posted on by Microsoft Employee

Whe I try to upload a file to an FTP Server I always get an error on "ftpWebRequest.GetRequestStream();"

The Error is:

System.Net.ProtocolViolationException: Can not send content-body with this verb-type
   at System.Net.FtpWebRequest.GetRequestStream()

I cannot find the problem in my code which is the following:

public void ftpUploadFile(Filename _file)
{
    System.IO.Stream        ioStream;
    System.IO.StreamReader  ioStreamReader, responseReader;
    System.String           strReadLine;
    System.Text.Encoding    utf8;
    System.Byte[]           bytes;
    FileIOPermission        permission;

    FilePath filePathDest;
    int length;
    ;
    if (_file != '')
    {
        try
        {
            permission = new FileIOPermission(_file,"RW");
            permission.assert();

            // BP Deviation Documented
            ioStreamReader = new System.IO.StreamReader(_file);
            utf8 = System.Text.Encoding::get_UTF8();

            if (ioStreamReader)
            {
                bytes = utf8.GetBytes( ioStreamReader.ReadToEnd() );

                ioStreamReader.Close();
                
                // Marshaling .NET to X++
                ftpObject = System.Net.WebRequest::Create(ftpHostName + @"/" + _file);
                ftpWebRequest = ftpObject;
                if (ftpWebRequest)
                {
                    ftpWebRequest.set_KeepAlive(false);
                    ftpWebRequest.set_UsePassive(true);
                    ftpWebRequest.set_Method(#UploadFile);
                    ftpWebRequest.set_UseBinary(true);
                    ftpWebRequest.set_Timeout(ftpTimeOut);
                    ftpWebRequest.set_ReadWriteTimeout(ftpTimeOut);
                    ftpWebRequest.set_ContentLength(bytes.get_Length());
                    this.setFTPCredentials();

                    ioStream = ftpWebRequest.GetRequestStream();
                    ioStream.Write(bytes, 0, bytes.get_Length());
                    ioStream.Close();
                    
                    webResponse = ftpWebRequest.GetResponse();
                    
                    responseReader = new System.IO.StreamReader(webResponse.GetResponseStream());
                    
                    info(responseReader.ReadToEnd());
                    
                    webResponse.Close();
                }
                 CodeAccessPermission::revertAssert();
            }
        }
        catch(Exception::CLRError)
        {
            exception = CLRInterop::getLastException();
            if(exception != null)
            {
                exception = exception.get_InnerException();
                checkFailed(exception.ToString());
            }
        }
    }
}


Any Ideas?

*This post is locked for comments

  • Suggested answer
    Brandon Wiese Profile Picture
    Brandon Wiese 17,786 on at
    RE: Upload Filet to FTP return Error

    Sending files over a web request should use the POST method.

    Try

    ftpWebRequest.set_method("POST");

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,104 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans