web
You’re offline. This is a read only version of the page.
close
Skip to main content
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,788 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Priya_K Profile Picture

Priya_K 4

#2
Alexey Lekanov Profile Picture

Alexey Lekanov 3

#3
Scott_itD Profile Picture

Scott_itD 2 Community Manager

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans