web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Upload Filet to FTP return Error

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Brandon Wiese Profile Picture
    17,790 on at

    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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans