【发布时间】:2011-05-25 11:32:52
【问题描述】:
我尝试将文件上传到 FTP 服务器上的目录。我将此方法与FtpWebRequest 一起使用。
我想为该用户上传一个文件到主目录,但我总是收到以下错误消息:
请求的 URI 对于此 FTP 命令无效。
可能有什么问题?我试过关闭被动模式,但还是一样。
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();
}
【问题讨论】:
-
该错误表明 12.22.44.45 上没有 ftp 服务器愿意接受 pokus 和密码的凭据(无论真实信息是什么)。您是否尝试使用这些设置/凭据在代码之外通过 ftp 到此服务器?
-
你能告诉我们你从哪里得到错误信息吗?即是什么语句导致了错误?
标签: c# .net ftp ftpwebrequest