C# FTP流请求流问题

C# FTP流请求流问题,c#,ftp,system.net.webexception,C#,Ftp,System.net.webexception,我得到的错误是web异常。请求的URI对此FTP命令无效 我不知道如何修理它。有人知道他的名字吗?谢谢 private void SendFile(FileInfo file) { Console.WriteLine("ftp://" + ipAddressTextField.Text); // Get the object used to communicate with the server. strin

我得到的错误是web异常。请求的URI对此FTP命令无效

我不知道如何修理它。有人知道他的名字吗?谢谢

private void SendFile(FileInfo file)
        {
            Console.WriteLine("ftp://" + ipAddressTextField.Text);
            // Get the object used to communicate with the server.
            string ftp = "ftp://" + ipAddressTextField.Text;
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp);
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential(usernameTextField.Text, passwordTextField.Text);


            // Copy the contents of the file to the request stream.
            byte[] fileContents = File.ReadAllBytes(file.FullName);
            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();
        }

似乎您没有为上载设置目标文件名,因此服务器没有用于上载文件的文件名

您可以使用以下内容将其设置为与源文件名相同:

string ftp = "ftp://" + ipAddressTextField.Text + "/" + file.Name;
Console.WriteLine(ftp);
为了在文件名可能有特殊字符的情况下创建更加健壮的Uri,您可以使用类来构建它,并将其传递给WebRequest.Create