使用FTPWebRequest上传期间“请求的URI无效”
我尝试将文件上传到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(); }
如果要上传某些内容, 则必须为FTPClient提供文件名 。
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://12.22.44.45/myNewFile.dat");
我建议你使用WebClient ,这是一个更高级别的抽象,使用HTTP和FTP,并且API更简单,性能也相同(使用相同的API)。
这是上传数据 。
上述就是C#学习教程:使用FTPWebRequest上传期间“请求的URI无效”分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1034684.html