且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

"请求URI是无效的"上载过程中的FtpWebRequest

更新时间:2022-10-23 18:43:36

如果您要上传的东西,你会have提供FTPClient一个文件名。

 的FtpWebRequest请求=(的FtpWebRequest)WebRequest.Create(ftp://12.22.44.45/myNewFile.dat);

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();

}

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");