The requested URI is invalid for this FTP command.

Vuyiswa Maseko 351 Reputation points
2022-03-07T09:12:36.29+00:00

Good Day All

i am uploading a file via ftp behind a Proxy and my code looks like this

        string HostName = "ftp://ftp.myserver.com";
        string UserName = "usr;
        string Password = "password;


                string PureFileName = new FileInfo("TotalAmount").Name;
                String uploadUrl = String.Format("{0}", HostName );
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uploadUrl);
                request.Method = WebRequestMethods.Ftp.UploadFile;
                // This example assumes the FTP site uses anonymous logon.  
                request.Credentials = new NetworkCredential("username", "password");
                request.Proxy = null;
                request.KeepAlive = true;
                request.UseBinary = true;
                request.Method = WebRequestMethods.Ftp.UploadFile;

                // Copy the contents of the file to the request stream.  
                StreamReader sourceStream = new StreamReader(FullPath);
                byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                sourceStream.Close();
                request.ContentLength = fileContents.Length;

                Stream requestStream = request.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

and when i run this i get an error

*The requested URI is invalid for this FTP command.*

i am doing this in a Windows service not a web project

Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,661 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jose Zero 576 Reputation points
    2022-03-08T15:21:20.203+00:00

    Since you behind a Proxy, and naturally behind NAT, as I know you have to use Passive Mode, you make a practical test with any FTP client.
    system.net.ftpwebrequest.usepassive

    0 comments No comments