Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Requested URI is invalid" during upload with FtpWebRequest

I trying upload file to a directory on a FTP server. I used this method with FtpWebRequest. I would like to upload one file to a home directory for this user, but I always get the following error message:

The requested URI is invalid for this FTP command.

What can be problem? I tried use passive mode off, but it still the same.

static void FtpUpload()
{


    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://12.22.44.45");
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.UsePassive = false;

    // This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential("pokus", "password");

    // Copy the contents of the file to the request stream.
    StreamReader sourceStream = new StreamReader(path);
    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);

    response.Close();

}
like image 789
redrom Avatar asked Dec 14 '10 18:12

redrom


2 Answers

If you want to upload something, you'll have to provide the FTPClient with a filename.

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://12.22.44.45/myNewFile.dat");
like image 182
Bobby Avatar answered Oct 22 '22 10:10

Bobby


I suggest you use WebClient which is a higher level abstraction and works with HTTP and FTP and has much simpler API and performance-wise pretty the same (uses the same API).

Here is upload data.

like image 42
Aliostad Avatar answered Oct 22 '22 11:10

Aliostad